Detect if all minecraft players are within a certain area
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.
-
Create a scoreboard objective to store the number of players outside the radius.
/scoreboard objectives add minigameData dummy
-
Create an always active repeating command chain of 4 blocks. Inside, place the following commands:
-
Reset the number of players outside the required range.
/scoreboard players set #playersOutsideRange 0
-
Tell everyone outside the range to add 1 to this value.
/execute @a[rm=10] ~ ~ ~ scoreboard players add #playersOutsideRange minigameData 1
-
Set a redstone block to a nearby location.
/setblock ? ? ? redstone_block
Replace the
?
s with the coordinates of a convenient location nearby.
-
-
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:
-
Test for zero players outside the range.
/scoreboard players test #playersOutsideRange minigameData 0 0
-
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.