Minecraft – Using Minecraft execute command to test for players near coordinates

minecraft-java-edition

I have been trying to test if a player is at specific coordinates using this command in a command block:

execute as @a[x=-166,y=4,z=-120,distance=3] run say cool  

But nothing happens when I trigger it. If I take the distance=3 part out it works even if I'm not at the coordinates stated.

What I'm doing wrong?

Best Answer

So I did some research, and it turns out that the way [x=X,y=Y,z=Z] works without a distance selector is different than what it used to be:

Selects targets based on distance to that exact position. Combine with selecting by radius to select only targets at that specific position.

Basically, distance is being automatically filled with the distance from the executing position (in this case, the command block) to the coordinates supplied.

When you use the selector form of distance=3, you're saying you want players that are exactly 3 metres away from the coordinates specified, and given that coordinates are floating point values, that's basically impossible without teleporting them to a specific position. Instead, you want to use a range, such as distance=2..3 (2 to 3 metres away), distance=..3 (less than/equal to 3 metres away), or distance=3.. (greater than/equal to 3 metres away).