I'm really confused with this, I'm trying to do a gamemode, but I get constantly errors. Right now I use: PHP: public $player = []; public function setBeast($game) { $server = $this->getServer(); $players = $server->getLevelByName($game)->getPlayers(); $this->beast[$game] = strtolower($players[array_rand($players)]->getName()); } public function getBeast($game) { return $this->beast[$game]; } But, when I try to check if player is the random selected player I get errors telling me that the array is undefined :/ Example: PHP: public function onPvP(EntityDamageEvent $eventPvP) { if ($eventPvP instanceof EntityDamageByEntityEvent) { $entity = $eventPvP->getEntity(); $damager = $eventPvP->getDamager(); $games = ["RB-1", "RB-2", "RB-3", "RB-4", "RB-5", "RB-6", "RB-7", "RB-8", "RB-9", "RB-10"]; if ($entity instanceof Player && $damager instanceof Player) { if ($eventPvP->getEntity()->getLevel()->getName() == "ZeSpawn") { $eventPvP->setCancelled(true); } else{ foreach ($games as $game) { if (strtolower($entity->getName()) != $this->beast[$game] and strtolower($damager->getName()) != $this->beast[$game] or $this->pvpEnabled[$game] == false) { $eventPvP->setCancelled(true); } } } } } } Also in my timer! - Large file in 3...2...1 Spoiler PHP: class TimerTask extends PluginTask{ public function __construct(Main $plugin, $game){ $this->plugin = $plugin; $this->game = $game; parent::__construct($plugin); } public function onRun($tick){ $this->plugin->gameSec[$this->game] -= 1; $level = $this->plugin->getServer()->getLevelByName($this->game); $players = $level->getPlayers(); if($this->plugin->gameSec[$this->game] <= 410 and $this->plugin->gameSec[$this->game] >= 396){ foreach($players as $p){ $seconds = $this->plugin->gameSec[$this->game] - 395; $p->sendPopup(TextFormat::WHITE . TextFormat::BOLD . "Game will start in " . $seconds); } } elseif($this->plugin->gameSec[$this->game] <= 394 and $this->plugin->gameSec[$this->game] >= 380){ foreach($players as $p){ $seconds = $this->plugin->gameSec[$this->game] - 379; $p->sendPopup(TextFormat::WHITE . TextFormat::BOLD . "Beast will be free in " . $seconds); } } elseif($this->plugin->gameSec[$this->game] == 390){ $this->plugin->setBeast($this->game); } elseif($this->plugin->gameSec[$this->game] <= 378){ foreach($players as $p){ $p->sendPopup("Game will end in " . $this->plugin->gameSec[$this->game] . " seconds"); } } switch($this->plugin->gameSec[$this->game]){ case 395: foreach($players as $p) { $p->sendTip("Game started"); $this->plugin->pvpEnabled[$this->game] = true; if ($this->plugin->getBeast($this->game) == strtolower($p->getName())) { $p->teleport(new Position($this->plugin->beastPosition[0],$this->plugin->beastPosition[1],$this->plugin->beastPosition[2],$this->plugin->getServer()->getLevelByName($this->game))); $this->plugin->addKit($p); } else{ $p->teleport(new Position($this->plugin->startPointPosition[0], $this->plugin->startPointPosition[1], $this->plugin->startPointPosition[2], $this->plugin->getServer()->getLevelByName($this->game))); } } break; case 379: foreach($players as $p){ $p->sendMessage(TextFormat::BOLD . TextFormat::WHITE . $this->plugin->beast[$this->game] . " was selected as beast!"); $this->plugin->addKit($this->plugin->beast[$this->game]); $p->sendTip("Beast is free"); if(strtolower($p->getName()) == $this->plugin->getBeast($this->game)){ $p->teleport(new Position($this->plugin->startPointPosition[0], $this->plugin->startPointPosition[1], $this->plugin->startPointPosition[2],$this->plugin->getServer()->getLevelByName($this->game))); } } break; case 0: $this->plugin->getServer()->broadcastMessage(TextFormat::WHITE . TextFormat::BOLD . "Runners won the game " . $this->game); foreach($players as $p){ $p->teleport($this->plugin->getServer()->getDefaultLevel()->getSafeSpawn()); } $this->plugin->stopGame($this->game); break; } }} I don't what happen, any player get teleported to the start point, and I think the *big* problem is the array. If you see another problem, only tell me, please!
To save a random player's name into an array, PHP: // Level $level$players = [];$lp = $level->getPlayers();$rand = $lp[rand(0, count($lp) - 1)]->getName();$players[$rand] = $rand;
Always, try doing a var_dump() on the array if you have its elements undefined. Or did you mean the whole array rather than the specific element to be undefined?
That method only works when hour array is ordered linearly, i.e. with array keys incrementing starting from 0.