I am making an minigame, and I need to detect if all players are nearby
/testfor @a[r=10] but this returns true even if only one player is nearby
How can I check if all players are near?
Arqade is a question and answer site for passionate videogamers on all platforms. It only takes a minute to sign up.Sign up to join this community
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
?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...
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.