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?


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...

I ended up using /testfor @a[rm=10] to check if there were any players outside the range, then inverting the comparator output from that.

The other answers here were a bit complicated, but they got me pointed in the right direction - I had completely forgotten that rm was a thing.

  • In 1.13+, this can be condensed into one command: /execute unless entity @a[distance=10..] run [...] – AMJ Aug 10 '20 at 22:14

The way I prefer to do this is with comparators.

Have a repeating command block testfor @a and place a comparator coming out of it.

Then have another repeating command block testfor @a[selector test criteria] with a comparator coming out of it.

Run the comparator from the first block into the side of the comparator of the second block. Have the the output of the second comparator run into the logic you want triggered when all players are present.

When all players are within the range specified by the second command block, the two comparator signals will be equal, meaning that a redstone signal will be sent to your trigger. If they're not all present, the first comparator signal is stronger, which blocks the output from the second command block triggering your logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.