3

I'm trying to test for a player in a certain area relative to the command block executing the command, so if I need to move the location of what I'm making, the command block isn't sensing for something 3000 blocks away. What I have is this:

/testfor @a[x=~1,dx=0,z=~-3,dz=0,y=~4,dy=102]

And this (these are 2 separate command blocks sensing 2 different areas):

/testfor @a[x=~1,dx=0,z=~3,dz=0,y=~4,dy=102]

But when I test them, they say "The entity UUID provided is in an invalid format." What am I doing wrong?

2
  • Try running the same command without the ~
    – user143228
    Commented Jun 13, 2016 at 17:53
  • That will mess up the entire structure and that won't be relative to the command block. P.S. Answers should be answers, not comments
    – Matthew
    Commented Jun 13, 2016 at 19:18

2 Answers 2

2

You cannot use the ~ character in target selectors, meaning you'd need to rely on command syntax and not selector syntax. You cannot use relative coordinates with /testfor as its command syntax does not support it.

You would instead need to place an entity at the desired position and execute from it relatively using /execute:

/execute <target> X Y Z <command>

<target> is the target entity to change the coordinate origin to, while XYZ is going to further modify the origin. Using relative coordinates would cause "command" to be executed relative from the target's position.

Sample armor stand to create:

/summon ArmorStand X Y Z {Tags:["anchor"]}

For example, assuming there is an armor stand tagged anchor at the intended location, the following tests for players relative to the armor stand:

/execute @e[type=ArmorStand,tag=anchor] ~1 ~4 ~-3 /testfor @a[dx=0,dz=0,dy=102]
/execute @e[type=ArmorStand,tag=anchor] ~1 ~4 ~3 /testfor @a[dx=0,dz=0,dy=102]
4
  • I did what you said, except my armor stands are named Up and Down, so my tags were tag=Up and tag=Down, but when I'm in the area where the /testfor command is sensing, nothing happens, the command block doesn't even show a message in the spot where errors usually show up.
    – Matthew
    Commented Jun 13, 2016 at 20:58
  • @Matthew If they're named via the CustomName tag, then you need to use the name parameter instead (@e[type=ArmorStand,name=Up]). Tags are a labeling feature introduced in 1.9; I've added an example of usage in the answer.
    – Skylinerw
    Commented Jun 13, 2016 at 21:01
  • one second, let me try that.
    – Matthew
    Commented Jun 13, 2016 at 21:03
  • still getting no result. is there a chance that the "true" signal would be coming from a location other than the command block?
    – Matthew
    Commented Jun 13, 2016 at 21:06
2

1.13+ update

As of 1.13+, command syntax has received an overhaul. It is now possible to execute a command at an arbitrary position using /execute positioned.

This means that a utility entity, such as an armour stand, is no longer required.

execute positioned ~1 ~4 ~-3 if entity @a[dx=0,dz=0,dy=102] run …
execute positioned ~1 ~4 ~3 if entity @a[dx=0,dz=0,dy=102] run …

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .