How do I choose 1 player out of 6 players to perform a task while the other 5 perform a different task?
$player = $players[mt_rand(0, count($players)-1]; When you use Level::getPlayers() or Server::getOnlinePlayers you cant do that for some strange reason, to bypass it do PHP: $numb = mt_rand(0, 5);$c = 0;foreach($level->getPlayers() as $p){if($numb == $c){$player = $p;break;}else{$c++;}}
Defining and performing incrementing on $c is redundant in this case as $c is tracking array keys but the foreach loop does this automatically PHP: foreach($array as $key => $value) # Or $c => $p in your case Your code can be shortened to PHP: $numb = mt_rand(0, 5);foreach($level->getPlayers() as $i => $p) { if($numb != $i) continue; return $p;} Or even shorter PHP: $players = $level->getPlayers();$p = $players[mt_rand(0, count($players) - 1)]; Because I don't see no reason why it should not work. Can you show a way to reproduce it?
Level::getPlayers()[0] always triggers the error undefined index. Oh sorry, I thought that is is just a one dimensional array
Why don't you use array_rand? PHP: $players = $this->getServer()->getOnlinePlayers();$random = $players[array_rand($players)];