How to use ~ in /testfor command block?

How to use ~ in /testfor command block? - Crop hacker silhouette typing on computer keyboard while hacking system

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?



Best Answer

You cannot use the "~" character in 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 with "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]



Pictures about "How to use ~ in /testfor command block?"

How to use ~ in /testfor command block? - From above of crop anonymous male hacker typing on netbook with data on screen while sitting at desk
How to use ~ in /testfor command block? - From below of ethnic boy in hoodie controlling robot using panel while standing in light room
How to use ~ in /testfor command block? - Elderly white hair worker using machine



How do you use the Testfor command in bedrock?

The /testfor command became obsolete in Minecraft 1.13, and has been replaced by the /execute command*. It will do the same as typing /testfor @e {SelectedItem:{id:"<item>"}} and then putting a comparator to the command block with the command, and then running the command which you wanna run.



Minecraft Redstone Tutorial | How to use testfor commands in MCPE!




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Anete Lusina, Sora Shimazaki, Vanessa Loring, Andrea Piacquadio