Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What am I doing wrong?

Comments in 'Plugin Development' started by MyNameIsTriXz, Jan 30, 2016.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I am trying to create a minigame, there I try to implement a function to let the player join a random game:


    PHP:
    public $games = ["Game1" => ["AbleToJoin", *player1*, *player2*...], "Game2" => ["InGame", *player1*, *player2*, *player3*...]];
    // *player* = players who are in this game

    public function joinGame($player){
    foreach(
    $this->games as $game){
    $level $this->getServer()->getLevelByName($game);
    if(
    count($this->game[$game]) > 1){ // game not full
    $player->teleport(new Vector3($level->getSafeSpawn));
    $player->sendMessage("You joined $game");
    return;
    }}
    There are coming errors that I give an array instead of a string (as example on getLevelByName(), but as you can "Game1" is a string...
    I would appreciate your help.
  2. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    what the ...
    you only foreached the array, so the foreached var will have arrays as well like you defined on the top, so it will return those array, not "Game1" "Game2" etc.
  3. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I know, is there anyway to convert it to a string?
  4. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    You're only getting the values(players) and not the key(game).
    PHP:
    foreach ($this->games as $game => $players){
    //todo
    //note $players give another array.
    }

Share This Page

Advertisement