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

Foreach error

Comments in 'Plugin Development' started by CyberPowerCrazii, Mar 24, 2016.

  1. CyberPowerCrazii
    Offline

    CyberPowerCrazii New Member

    Joined:
    Jan 9, 2016
    Posts:
    8
    Minecraft User:
    GlitchPlayer90
    Please help me with this code, it seems i cannot get it right.

    public function onJoin(PlayerJoinEvent $event){$player = $event->getPlayer();foreach($this->getConfig()->get("joinmessages") as $message){$player->sendMessage($message); }
  2. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Can you show us the config? Also, did you register events?
    applqpak likes this.
  3. CyberPowerCrazii
    Offline

    CyberPowerCrazii New Member

    Joined:
    Jan 9, 2016
    Posts:
    8
    Minecraft User:
    GlitchPlayer90
    ---
    joinmessages:
    - "§c Welcome to Wyvern High
    - "§c Welcome to Wyvern High
    ...

    Yes
  4. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    Put double-quotes after each message.
    Code:
    ---
    joinmessages:
      - "§c Welcome to Wyvern High"
      - "§c Welcome to Wyvern High"
    ...
    
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Please do not say "it is not working" or "it does not seem to work right". Instead, say how it is not working, for example, "no error messages on console but nothing happened", etc. Otherwise, it is nothing more meaningful than "I have a bug".
    HotFireyDeath and applqpak like this.
  6. CyberPowerCrazii
    Offline

    CyberPowerCrazii New Member

    Joined:
    Jan 9, 2016
    Posts:
    8
    Minecraft User:
    GlitchPlayer90
    This is an error on console "invalid argument supplied for foreach"
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Then that means your subject of foreach, that is $this->getConfig()->get("joinmessages"), is not an array. You should use var_dump($this->getConfig()->get("joinmessages")) to find out what $this->getConfig()->get("joinmessages") is, and find out why it isn't an array. Please, try to debug it yourself before asking people from the Internet. In the end it is you yourself who have to help yourself. We are not mind-readers or hackers who know what is happening in your server.
  8. KubixQ
    Offline

    KubixQ Active Member

    Joined:
    Sep 13, 2015
    Posts:
    85
    Minecraft User:
    KubixQ
    Use [php]&&[/php] tags to post your code, this looks a lot clearly.
    PHP:
    public function onJoin(PlayerJoinEvent $event){
    $player $event->getPlayer();
    foreach(
    $this->getConfig()->get("joinmessages") as $message){
    $player->sendMessage($message);
    }
    applqpak likes this.
  9. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Try this.

    PHP:
    public function onJoin(PlayerJoinEvent $event)
    {

      
    $player $event->getPlayer();

      foreach(
    $this->getConfig()->get("joinmessages") as $key => $message)
      {

        
    $player->sendMessage($message);

      }

    }
    Vaivez66 likes this.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I wonder what is changed at all?
  11. CyberPowerCrazii
    Offline

    CyberPowerCrazii New Member

    Joined:
    Jan 9, 2016
    Posts:
    8
    Minecraft User:
    GlitchPlayer90
  12. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Look at the foreach statement. :p
  13. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    The $key will not really matter in this situation, as he is getting the $message.
    PEMapModder and applqpak like this.
  14. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Ah ok.

Share This Page

Advertisement