How do I check if a player is at specific coordinates? [duplicate]

Solution 1:

You're scanning for the nearest player, your should scan for all the players.

Solution

/testfor @a[x=X,y=Y,z=Z,r=R]

Where:

  • X the x location
  • Y the y location
  • Z the z location
  • R the radius

For example:

/testfor @a[x=64,y=10,z=64,r=5]

(Search for players on coordinates 64,64 at height 10, with a radius of 5 blocks)

Variables

Specify the location:

  • @p nearest player
  • @r random player
  • @a all players
  • @e all entities

Specify the values:

  • x, y, z coordinate
  • r, rm radius (max, min)
  • m game mode
  • c count
  • l, lm experience level (max, min)
  • score_name max score
  • score_name_min min score
  • team team name
  • name entity name
  • dx, dy, dz volume dimensions
  • rx, rxm vertical rotation (max, min)
  • ry, rym horizontal rotation (max, min)
  • type entity type

Examples:

To test if Alice is online:

testfor Alice

To count the number of players in survival mode within a 3-block radius of (0,64,0):

testfor @a[0,64,0,3,m=0]

To count the number of players currently flying:

testfor @a {abilities:{flying:1b}}

To count the number of zombies within a 20-block radius of (0,64,0):

testfor @e[0,64,0,20,type=Zombie]

Source

Solution 2:

In newer versions of minecraft you can use something like this:

/execute as @a[x=100,y=100,z=100,dx=0,dy=0,dz=0] at @s run <command>

You need to specify delta values for this to work. The coordinates defined with x y z are one corner of the specified area, the delta values dx dy dz define how many blocks the second corner is away from the first corner.

You can also use this command:

/execute positioned 100 100 100 as @a[distance=..1] at @s run <command>

This will execute your command as all players who are within a 1 block radius from the coordinates 100 100 100.