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

Spawn Players on different positions?

Discussion in 'Development' started by Deeonix, Jan 22, 2017.

  1. Deeonix

    Deeonix Spider Jockey

    Messages:
    29
    How do i spawn Players on different positions?
     
  2. kaliiks

    kaliiks Zombie

    Messages:
    250
    PHP:
    foreach($this->getServer()->getOnlinePlayers() as $p){
    $p[1]->teleport($x$y$z);
    $p->[2]->teleport($x1$x1$z1);
    $p->[3]->teleport($x2$x2$z2);
    }
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Uhhh, I don't think that code will work lol.

    What you could do is, on player join, teleport them to a random position from an array. For example:
    PHP:
    public function onJoin(PlayerJoinEvent $event) {
      
    $positions = []; // Fill this in with the positions you want them to spawn at
      
    $event->getPlayer()->teleport($positions[array_rand($positions)]);
    }
     
  4. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Learn PHP
     
  5. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Just wtf? why do you try to use the language construct foreach, which as the name implies does something for each item just to the try to access the array directly? also $p->[1] is totally wrong and you should know why. This is one of the few cases where you really have to JUST learn php/general oop first. please
     
    Marabou likes this.
  6. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    you need to learn PHP



    you can teleport the players with this:

    PHP:
    //define coords (x, y, z)
    $coords = [
    [
    5,98,11],
    [
    46,84,4],
    [
    2,4,89]
    ];
    foreach(
    $this->getServer()->getOnlinePlayers() as $p){
        
    //get coords index by getting the index of Server::getOnlinePlayers();
        
    if(isset($coords[array_search($p$coords)])){
            
    //define coords data for the player
            
    $data $coords[array_search($p$coords)];
            
    $x$data[0];
            
    $y$data[1];
            
    $z$data[2];
            
    $p->teleport($x$y$z);
        } else {
            
    $p->sendMessage("no coords found at index " array_search($p$coords));
        }
    }

    //not tested but if it does not work then try this:








    $coords = [
    [
    5,98,11],
    [
    46,84,4],
    [
    2,4,89]
    ];
    $index 0;
    foreach(
    $this->getServer()->getOnlinePlayers() as $p){
        
    //get coords
        
    if(isset($coords[$index])){
            
    //define coords data for the player
            
    $data $coords[$index];
            
    $x$data[0];
            
    $y$data[1];
            
    $z$data[2];
            
    $p->teleport($x$y$z);
        } else {
            
    $p->sendMessage("no coords found at index " $index);
        }
        
    $index++;
    }

     
  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.