# How to execute a command when ALL of the players are close enough

I'm making an adventure map and came to a point where all players have to be within three blocks to activate a command.

I tried using the /execute command on a repeating, always active command block but that command executed as soon as one player came within three blocks.

execute if entity @a[distance=..3] run tp @a -424.5 80.1 108.5 135 0

• I have no idea why but this commands works (I've inverted it two times and added the specific coordinates): `/execute unless entity @a[x=-460,y=81,z=108,distance=3..] run tp @a -424.5 80.1 108.5 135 0` -- someone can post this as an answer since I don't know how, feel free to take the free "+1 answered question" thing if that exists. c: Jun 18 '19 at 13:23
• I'd stick with Fabian's way, your's seems a bit sketchy. Jun 18 '19 at 13:38
• It's the same command... I only added coordinates relative to where the distance has to be from, if you understand what I mean. But I really appreciate his answer! Jun 18 '19 at 13:49

If all players are nearby, that means that no players are far away. In command form:

``````/execute unless entity @a[distance=3..] run <command>
``````

But that still executes if someone is in other dimensions, you can fix that this way:

``````/execute unless entity @a[distance=3..] in the_nether unless entity @a[distance=0..] in the_end unless entity @a[distance=0..] run <command>
``````

If you want the command to execute in the overworld, you have to add `in overworld ` before `run`.

• Posted my answer just to find out you posted a better one 20 seconds faster. Oh well :) Jun 18 '19 at 13:30

This reason yours is not working is that it is testing for all the players in that area.

# solution 1

If you want to execute a command if all of the players are within range. You probably need to use scoreboards. First would need to create a scoreboard.

`/scoreboard objectives add PlayerCount dummy "Number of Players"`

Next you either need to have a datapack function that repeats every tick or 2 repeating command blocks which perform these commands

`/execute store result score PlayersInWorld PlayerCount run execute if entity @a` (Stores the number of players in the world into PlayersInWorld constant)

`/execute store result score PlayersInArea PlayerCount run execute if entity @a[distance=..3]` (Stores the number of players in the range into PlayersInArea constant)

And finally, you test if all the players in the Minecraft world are in that spot by doing

`/execute if score PlayersInArea PlayerCount = PlayersInWorld PlayerCount run tp @a -424.5 80.1 108.5 135 0`

# solution 2

That is a rather complex solution. Here's a much more simple one, which tests if there are no players outside of the radius rather than testing if all the players are in the radius.

`/execute unless entity @p[distance=3..] run tp @a -424.5 80.1 108.5 135 0`

Side Note

By using the first solution you can display the scoreboard which would help you know how many players still need to go into the area. You could also replace PlayersInWorld to PlayersRequired and set it to a specific number so that rather than needing all of the players you only need a certain amount.

• Wow, that's complicated. And the second solution is just a copy of mine, with the dimension problem still in it. You can also vary that one to display the number of players outside: `/execute as @e[distance=3..] run scoreboard players add dummyName scoreboardName 1` Jun 19 '19 at 5:40