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

Undefined offset and array to string conversion error

Discussion in 'Development' started by gsdgamer, May 20, 2017.

  1. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Hi all. I am trying to get if a player joins my server they spawn in the hub each time they join, but I keep getting this error.

    Code:
    Undefined offset: 3 in C:\Users\user\Documents\PocketMine x64\plugins\Basic\src\Basic\Main.php on line 67
    
    Code:
    Array to string conversion in phar://C:/Users/user/Documents/PocketMine x64/PocketMine-MP.phar/src/pocketmine/level/Position.php on line 108
    My code for Main is:
    Code:
    public function onjoin(PlayerJoinEvent $pje){
            $player = $pje->getPlayer();
            $name = $player->getName();
            
            $spawnloc = array($this->getConfig()->getNested("SPAWN_POS"));
            
            if($this->getConfig()->get("FORCEHUB") === TRUE && $spawnloc[3] !== $player->getLevel()->getName()){
                $player->teleport(new Position($spawnloc[0], $spawnloc[1], $spawnloc[2], $spawnloc[3]));
            }
        }
    I want to check the config file for the x,y,z and level position and make sure if the player is not in the level when joining, they teleport to the hub.
    Could someone point me in the right direction. Thanks
     
  2. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    That means 3 isn't in your config. Please learn PHP and google errors before you make a forum post over a simple error.
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Assuming your config to be like this:
    Code:
    SPAWN_LOC:
    - 100
    - 256
    - 50
    - "world"
    
    You are putting an array inside an array.
    PHP:
    $spawnloc = array($this->getConfig()->getNested("SPAWN_POS"));
    So $spawnloc looks something like this:
    Code:
    $spawnloc = [
        0 => [
            0 => 100,
            1 => 256,
            2 => 50,
            3 => "world"
        ]
    ];
    
    There's no need of putting an array inside an array.
    PHP:
    $spawnloc $this->getConfig()->getNested("SPAWN_POS");
    $spawnloc[3] = Server::getInstance()->getLevelByName($spawnloc[3]);
    if(
    $spawnloc[3] !== NULL){
        
    $player->teleport(new Position(...$spawnloc));
    }
     
    Michael and jasonwynn10 like this.
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Facepalm section? definitely
     
  5. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Ok thanks for the info and Jasonwynn10 instead of just typing Facepalm section on every post why not just try and help.
     
    [deleted] likes this.
  6. [deleted]

    [deleted] Guest

    I think he tries to help, he just meant that the Question is a Question for beginners and should be because of that moved in there.
     
    jasonwynn10 likes this.
  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.