I have a problem with floatingtext. When a player is not in world DuelsMap1 is no should write "No one plays", but somehow the flying text is not present. But when the player comes into the world, flying text is creating and writes that players 1, but when I exit it still says that players 1 although there is no one there. Why? My code: PHP: foreach($this->getServer()->getLevelByName("DuelsMap1")->getPlayers() as $players){ $online = count($players); if($online == 0){ $this->particle->setTitle("§cNo one plays"); $this->particle->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle, [$players]); $this->particle1->setTitle("§bDuеls"); $this->particle1->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle1, [$players]); $this->particle2->setTitle("§e§lCLICK TO PLAY"); $this->particle2->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle2, [$players]); }else{ $this->particle->setTitle("§e{$online} Plаying"); $this->particle->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle, [$players]); $this->particle1->setTitle("§bDuеls"); $this->particle1->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle1, [$players]); $this->particle2->setTitle("§e§lCLICK TO PLAY"); $this->particle2->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle2, [$players]); } };
PHP: foreach($this->getServer()->getLevelByName("DuelsMap1")->getPlayers() as $players){ $online = count($this->getServer()->getLevelByName("DuelsMap1")->getPlayers()); if($online == 0){ $this->particle->setTitle("§cNo one plays"); $this->particle->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle, [$players]); $this->particle1->setTitle("§bDuеls"); $this->particle1->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle1, [$players]); $this->particle2->setTitle("§e§lCLICK TO PLAY"); $this->particle2->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle2, [$players]); }else{ $this->particle->setTitle("§e{$online} Plаying"); $this->particle->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle, [$players]); $this->particle1->setTitle("§bDuеls"); $this->particle1->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle1, [$players]); $this->particle2->setTitle("§e§lCLICK TO PLAY"); $this->particle2->setText(""); $this->getServer()->getDefaultLevel()->addParticle($this->particle2, [$players]); } } $online fix
Way more efficient code. There is no need to run the count() and adding each particle to all the players in that foreach loop. PHP: $players = $this->getServer()->getLevelByName("DuelsMap1")->getPlayers();$online = count($players);$lobby = $this->getServer()->getDefaultLevel();if ($online == 0) { $this->particle->setTitle("§cNo one plays"); $this->particle->setText(""); $this->particle1->setTitle("§bDuеls"); $this->particle1->setText(""); $this->particle2->setTitle("§e§lCLICK TO PLAY"); $this->particle2->setText("");} else { $this->particle->setTitle("§e" . $online . " Plаying"); $this->particle->setText(""); $this->particle1->setTitle("§bDuеls"); $this->particle1->setText(""); $this->particle2->setTitle("§e§lCLICK TO PLAY"); $this->particle2->setText("");}$lobby->addParticle($this->particle, [$players]);$lobby->addParticle($this->particle1, [$players]);$lobby->addParticle($this->particle2, [$players]);