Solved How to get the number of players of a world?

Discussion in 'Development' started by Minetron26, Nov 7, 2018.

  1. Minetron26

    Minetron26 Spider Jockey

    Messages:
    38
    GitHub:
    minetron-26
    Hi!, I was wondering how and if it is possible to get the number of players in a world, is it possible?, and if so, how do I do it?
     
  2. notdrewdev

    notdrewdev Silverfish

    Messages:
    23
    GitHub:
    drewsucksatlife
    It is possible -
    PHP:
    count($level->getPlayers());
    this will return the count for you.
     
  3. Minetron26

    Minetron26 Spider Jockey

    Messages:
    38
    GitHub:
    minetron-26
    Yes, I've already tried that but it always says the same thing: Call to a member function getPlayers() on null
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member Verified

    Messages:
    2,129
    GitHub:
    Thunder33345
    make sure $level is instance of Level
    betting $level is null not Level
     
  5. Minetron26

    Minetron26 Spider Jockey

    Messages:
    38
    GitHub:
    minetron-26
    I do not understand what you mean
     
  6. Palente

    Palente Witch

    Messages:
    74
    GitHub:
    Palente
    $level must be an instanceof Level and not a string
    PHP:
    # i think you want to get players in a level with a level name so
    $level $this->getServer()->getLevelByName("level his name");
    #check if level exist
    if($level instanceof Level){
    #$level is an instanceof level so put your code
    $numberOfPlayer count($level->getPlayers());
    }else{
    #the level with that name is not available
    }
     
    Last edited: Nov 10, 2018 at 3:06 PM
    xXNiceAssassinlo YT likes this.
  7. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    571
    Or is not loaded.
     
  8. korado531m7

    korado531m7 Witch Verified

    Messages:
    71
    GitHub:
    korado531m7
    PHP:
    $worldName 'Level Name';
    if(
    $this->getServer()->isLevelLoaded($worldName)){
        
    $level $this->getServer()->getLevelByName($worldName);
        
    $count count($level->getPlayers());
    }
    echo 
    $count//example: 16
     
  9. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    627
    GitHub:
    xXNiceYT
    If Level isn’t loaded it would give u error undefined $count
     
  10. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    571
    $count = 0; before the code.
     
  11. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    627
    GitHub:
    xXNiceYT
    Ik I was telling him it won’t work
     
  12. Minetron26

    Minetron26 Spider Jockey

    Messages:
    38
    GitHub:
    minetron-26
    Ohh, that's what he meant, well you're right, I had not done that. Thanks.
     
    Last edited: Nov 12, 2018 at 12:46 AM

Share This Page

  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.