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

Solved Online players

Discussion in 'Development' started by JarguarLoveMC, Aug 28, 2019.

  1. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    Not all online players get item, only 1 random people get it. Is it because i use the mt_rand function?


    PHP:
    foreach(Server::getInstance()->getOnlinePlayers() as $online) {
    if(
    mt_rand(1100) === 1){
                    
    $money 50000;
                    
    EconomyAPI::getInstance()->addMoney($online$money);
                    
    $online->sendMessage(TF::GRAY " You received " TF::YELLOW "Easy money boo");             
                        } 
           }
                 
     
  2. Gianluxx

    Gianluxx Slime

    Messages:
    94
    GitHub:
    Gianluxx
    Code:
    if(mt_rand(1, 100) == 1){
    foreach(Server::getInstance()->getOnlinePlayers() as $online) {
                    $money = 50000;
                    EconomyAPI::getInstance()->addMoney($online, $money);
                    $online->sendMessage(TF::GRAY . " You received " . TF::YELLOW . "Easy money boo");             
                        }
    }
     
  3. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    That’s a very inefficient method of choosing a random player. Also, don’t call the server instance like that. Do it through your plugin, since it extends plugin base.

    Anyways, here’s what you’re wanting:

    PHP:
    // This is choosing random player
    $player $this->getServer()->getOnlinePlayers()[array_rand($this->getServer()->getOnlinePlayers())]
    Now do what ever you want with the player object.
     
  4. Gianluxx

    Gianluxx Slime

    Messages:
    94
    GitHub:
    Gianluxx
    He was not trying to get 1 random player... He was trying to give everyone Money if 1/100 happens
     
    JarguarLoveMC likes this.
  5. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
     
  6. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    Sorry for my miscommunication but what i meant is "not all online players get the money"

    what i want is for all online players to get the money
     
  7. Fadhel

    Fadhel Witch

    Messages:
    68
    GitHub:
    dimbis
    PHP:
    foreach($this->getServer()->getOnlinePlayers() as $players){
    $players->sendMessage("You got money");
    $this->getServer()->getPluginManager()->getPlugin("EconomyAPI")->addMoney($players5000);
     
  8. JarguarLoveMC

    JarguarLoveMC Spider Jockey

    Messages:
    49
    The code by @Gianluxx already helped me, thanks tho
     
  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.