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

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

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

  1. Minetron26

    Minetron26 Witch

    Messages:
    55
    GitHub:
    minetron26
    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 Spider Jockey

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

    Minetron26 Witch

    Messages:
    55
    GitHub:
    minetron26
    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

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

    Minetron26 Witch

    Messages:
    55
    GitHub:
    minetron26
    I do not understand what you mean
     
  6. Palente

    Palente Slime

    Messages:
    75
    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
  7. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Or is not loaded.
     
  8. korado531m7

    korado531m7 Slime

    Messages:
    77
    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:
    499
    GitHub:
    xXNiceYT
    If Level isn’t loaded it would give u error undefined $count
     
  10. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

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

    xXNiceAssassinlo YT Zombie Pigman

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

    Minetron26 Witch

    Messages:
    55
    GitHub:
    minetron26
    Ohh, that's what he meant, well you're right, I had not done that. Thanks.
     
    Last edited: Nov 12, 2018
  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.