i am working on a minigame but i want to know how i can work when the first two write command to enter the game no one can enter after them
This is very unreliable and unsustainable. What if it doesn't have a unique world?? And what if you want to add extra players into the world, such as moderators, spectators, etc.?
PHP: // $players_on - number of players on the minigame// if player enters minigameif($players_on < 2) { if($player->hasPermission("minigame-watcher") { // enter as watcher } else { // enter as player $players_on++; }} else { if($player->hasPermission("minigame-watcher") { // enter as watcher } else { // 2 players entered already }}
You can just create player array and then count number of players in it PHP: /** @var Player[] */public $players= []; $this->players[strtolower($p->getName())]= $player; //Add player to the array Now you get number of players, and check if there is more than 2 players, like this: PHP: $players = intval(count($this->players));if($players > 2){ //Player can not enter}else{ //Player can enter}
count() returns an int. Why intval... And your code will allow three players to join because $player is the current count not the new count. And without defining $players_on your post didn't explain anything at all.
Ok...since some member were trying to help... Take a look at this HERE Something maybe outdated but still usefull