Hey, Iv'e tried to use: PHP: $alivePlayers = count($levelArena->getPlayers()->getGamemode(0)); $alivePlayers is defining the number of players in the arena that are still alive (in game-mode 0) and NOT in game-mode 3 (Spectator)... but it gives me this error: PHP: [10:15:40] [Server thread/CRITICAL]: Could not execute task Atomization\SkyWars\GameSender: Call to a member function getGamemode() on array[10:15:40] [Server thread/CRITICAL]: Error: "Call to a member function getGamemode() on array" (EXCEPTION) in "SKYWARS/src/Atomization/SkyWars/Main" at line 932 Can someone help me Understand the error so i dont need to ask about it again. Here is another thread that I looked at but didn't find a solution. https://forums.pmmp.io/threads/player-count.4053/ I also have written: This will simply teleport the players back to spawn on game end. PHP: if($alivePlayers == 1){ foreach($playersInArena as $pl){ $pl->getInventory()->clearAll(); $pl->getArmorInventory()->clearAll(); $pl->removeAllEffects(); $pl->teleport($this->plugin->getServer()->getDefaultLevel()->getSafeSpawn()); $pl->setHealth(20); $pl->setFood(20); $pl->setNameTag($pl->getName()); if(!empty($this->plugin->api)){ $this->plugin->api->addMoney($pl,$money); } }}
$levelArena->getPlayers() will probably return an array of player objects so you need to loop through each player, retrieve the gamemode and count if gamemode is 0.
nope, didnt work... get same error: PHP: [10:15:40] [Server thread/CRITICAL]: Could not execute task Atomization\SkyWars\GameSender: Call to a member function getGamemode() on array[10:15:40] [Server thread/CRITICAL]: Error: "Call to a member function getGamemode() on array" (EXCEPTION) in "SKYWARS/src/Atomization/SkyWars/Main" at line 932 and to clarify some things up... $levelArena is defined in the code as: PHP: $levelArena = $this->plugin->getServer()->getLevelByName($arena);
Nope, something like learn basic programming: PHP: $count = 0;foreach ($levelArena->getPlayers() as $player) { if ($player->getGamemode() === 0) { $count++; }} ^ That is assuming that "$levelArena->getPlayers()" returns an array of "Player" objects.
Look at https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/command/defaults/ListCommand.php
Thank you all you your help! i was able to find a solution to my problem with @Tee7even advice.. also, i had and did try other variants of my code before you posted here. so i was still working on a fix so thanks again!
Here's a single liner: PHP: $playersAlive = count(array_filter($levelArena->getPlayers(), function($p){return $p->getGamemode() === 0;}));
Ha..! PHP: $count = 0; foreach ($levelArena->getPlayers() as $player) { if ($player->getGamemode() === 0) { $count++; } } What I mean is that it's not exactly a single liner. PHP: $playersAlive = count(array_filter($levelArena->getPlayers(), function($p) { return $p->getGamemode() === 0;}));