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

How to subtract from a count

Discussion in 'Development' started by rektpixel, Dec 19, 2017.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Hello, I'm making a game and I'm just a little confused onQuit event. What I'm trying to do is if a player quits the game it will message each player in the game saying '$player left the game. ($count/24)'
    With the code I have I'm getting this when the player joins and quits:
    '$player joined the game. (2/24)'
    '$player left the game. (2/24)'
    See the problem? because the player is still in the world when the message is sent its still counted as 2 when it should be 1. so I need to know how to subtract 1 from the count. this is what I have in QuitEvent:
    PHP:
        public function onQuit(PlayerQuitEvent $event)
        {
            
    $player $event->getPlayer();
            
    $level $this->getServer()->getLevelByName($this->currentLevel);
            
    $levelArena $level;
            
    $playersArena $levelArena->getPlayers();
            
    $gamecount count($this->getServer()->getLevelByName($this->currentLevel)->getPlayers(-1));
            foreach(
    $playersArena as $pl)
            
    $pl->sendMessage(TextFormat::YELLOW $player->getNameTag() . TextFormat::YELLOW " left the game. (" TextFormat::AQUA $gamecount TextFormat::RESET TextFormat::AQUA "/24" TextFormat::YELLOW ")");
        }
    }    
     
  2. McpeBooster

    McpeBooster Baby Zombie

    Messages:
    190
    GitHub:
    mcpebooster
    Replace
    PHP:
     $gamecount count($this->getServer()->getLevelByName($this->currentLevel)->getPlayers(-1)); 
    with
    PHP:
     $gamecount count($this->getServer()->getLevelByName($this->currentLevel)->getPlayers()) -1;
     
  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.