1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to send a message to a player (Not player who ran command)

Discussion in 'Facepalm' started by HittmanA, Jan 13, 2017.

  1. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    I need to send a message to a different player than sent the command. Lets see the command is /w <player> . How can I send a message to the <player> that was specified?
     
  2. applqpak

    applqpak Spider Jockey

    Messages:
    27
    GitHub:
    applqpak
    This is really easy...
    Just check that the first argument isn't null then run Server::getPlayer on it, which returns a Player object(if it doesnt fail).
     
    HimbeersaftLP and HittmanA like this.
  3. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    Code for what @applqpak said
    PHP:
    $player $this->getServer()->getPlayer($args);
    if(
    $player->isConnected()){
    $player->sendMessage("Hello!");
    }else{
    $sender->sendMessage("Player isn't online!");
    // "$sender" is the command sender
    }
     
    HittmanA likes this.
  4. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    Thanks!
     
  5. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    As far as I know Server::getPlayer() returns null if no player was found, which also means $player->isConnected() won't work in that case.
    That's how I would solve it:
    PHP:
     if($player instanceof Player){} 
    or
    PHP:
     if($player !== null){} 
    Besides that +1 :)
     
    HimbeersaftLP, jasonwynn10 and Muqsit like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.