Minecraft – Detect if all minecraft players are within a certain area

minecraft-bedrock-editionminecraft-commands

I am making an minigame, and I need to detect if all players are nearby

I tried /testfor @a[r=10] but this returns true even if only one player is nearby

How can I check if all players are near?

Best Answer

In order to do this, you'll need to invert the check and the arguments.

Your old test: If there are any players in the radius, run the command.
Required new test: If there are any players outside the radius, do not run the command, otherwise run it.

  1. Create a scoreboard objective to store the number of players outside the radius.

    /scoreboard objectives add minigameData dummy
    
  2. Create an always active repeating command chain of 4 blocks. Inside, place the following commands:

    1. Reset the number of players outside the required range.

      /scoreboard players set #playersOutsideRange 0
      
    2. Tell everyone outside the range to add 1 to this value.

      /execute @a[rm=10] ~ ~ ~ scoreboard players add #playersOutsideRange minigameData 1
      
    3. Set a redstone block to a nearby location.

      /setblock ? ? ? redstone_block
      

      Replace the ?s with the coordinates of a convenient location nearby.

  3. Create an impulse command chain of 2 blocks. Place the starting block next to the location you specified in the last command. Place these commands inside:

    1. Test for zero players outside the range.

      /scoreboard players test #playersOutsideRange minigameData 0 0
      
    2. CONDITIONAL: Run the command.

      ...Whatever you want...
      
Related Topic