I am trying to test for if a player is at a certain x and z, but not a certain height, and I can't find anything that says how to do it. Here's what I have:

/testfor @a[x=-50,z=79,r=1]

The problem is, it's only true if the player is standing on the command block, and I need it to be true if the player is anywhere directly above the command block, like five blocks above. What am I doing wrong?


You could do this by using the cubic volume selectors (dx,dy,dz) rather than a radius.

For example, to test for players around (-50, 79) with any height (or rather, a height from -5000 to 5000):

/testfor @a[x=-51,dx=2,z=78,dz=2,y=-5000,dy=10000]

This tests for anyone with x from -51 (x) to -49 (x + dx), a z from 78 (z) to 80 (z + dz), and y from -5000 (y) to 5000 (y + dy)

  • 1
    This will not work because y is still going to default to the sender's position (the command block), while dy will default to 0. The player will still have to be at the same height as the command block.
    – Skylinerw
    Jun 8 '16 at 18:19
  • 1
    @Skylinerw My bad, only tested the command in chat. Should be (kind of) fixed now, but your command is the better idea.
    – SirBenet
    Jun 8 '16 at 18:29

Unspecified origin parameters will default to the sender's location, being the command block. You can change the origin to be that of the player so that the x, y, and z parameters default to their position instead of the command block.

/execute @a 50 ~ 79 /testfor @a[c=1,r=1]
  • 1
    A note to OP that this will count the same player multiple times if other players are on the same y level, so be careful using this for output signal strength or commands other than /testfor (shouldn't matter if your commands are only for use on singleplayer, though).
    – SirBenet
    Jun 8 '16 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.