how can i make that when a player spawn or respawn, it appears aleatorymant on x spawn different Thank for you reply ^~^
There are 2 ways.. 1 Way You can register random spawn and then teleport player into it Example: PHP: /** @var array $spawns */public $spawns = [];/** @var int $count */public $count = 0;//Register spawnspublic function registerSpawns(){ $this->spawns["spawn1"] = new Vector3(1,1,1); $this->spawns["spawn2"] = new Vector3(2,2,2); $this->spawns["spawn3"] = new Vector3(3,3,3);} To teleport player to random spawn you can do something like this: PHP: public function selectSpawn(Player $p){ $spawn = $this->spawns["spawn" . mt_rand(1,3)]; if($spawn instanceof Vector3){ //Check if that spawn is registred $p->teleport($spawn); }} 2 Way You can just select random x,y,z instead of saving positions Example: PHP: public function selectSpawn(Player $p){ $x = mt_rand(50,80); //Select value between 50,80 $y = 30; $z = mt_rand(30,100); //Select value between 30,100 $position = new Vector3($x, $y, $z); $p->teleport($x,$y,$z);}
PHP: $spawnPoint = rand(1, 3);if($this->spawnPoint == 1){ $player->teleport(new Vector3(-160, 51, -120)); $player->sendMassage("spawn 1");}elseif($this->spawnPoint == 2){ $player->teleport(new Vector3(-120, 51, -120)); $player->sendMessage("spawn 2");}elseif($this->spawnPoint == 3){ $player->teleport(new Vector3(-150, 51, -120)); $player->sendMassage("spawn 3");} This is correct ?
Your code looks correct. You can use PlayerLoginEvent if the player joins and PlayerRespawnEvent if the player respawns. If you want do a completely random spawn: PHP: /** * @var Player $player */$randX = mt_rand(-200, 200); // chooses a random X position between -200 and 200.$randY = mt_rand(0, 128); // chooses a random Y position between 0 and 128.$randZ = mt_rand(-200, 200); // chooses a random Z position between -200 and 200.$randPos = new Vector3($randX, $randY, $randZ); // create a Vector3$player->teleport($randPos); // teleport the player to the random position.
something like the first way but add it like $spawns[] = ... and use $rand= $spawns[mtrand(0,count($spawns))]
Y-coordinate shouldn't be a static or a random number unless that's your aim. Set Y as Level::getHighestBlockAt(x, z) + 1.