Forgive me if this was already answered, but I couldn't find a similar question.

In 1.12.2 I'd like to teleport a player from (relative position to command block) to (another relative position to command block), set to "repeating" and "always active". The solution for absolute coordinates is:

/execute @a[x=a,y=b,z=c,r=d] ~ ~ ~ tp @a e f g

I'm rather surprised, since I'd think the general case of relative source and relative destination would be asked more frequently. Any help is appreciated!

New contributor
Andrew is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • This is in a command block? Why do you need relative coordinates then? Do you clone the command block to different places? – Fabian Röling Nov 8 at 7:37
  • 1
    Yes I'd like to clone it several times. It will also be part of a schematic, such that if I decide to move it somewhere else the command will remain relative to the build. – Andrew Nov 8 at 16:01
up vote 0 down vote accepted

1.12

This is actually a surprisingly hard thing to do with 1.12 commands. The only way I found is with a marker entity, the command inside the command block is then:

summon armor_stand ~ ~ ~ {Tags:["teleportMarker"],NoAI:1,NoGravity:1,Invisible:1,Marker:1}

Execute this command inside the command block only once per block. How you do that depends on your system.

Then tag everybody in your first area:

/execute @e[tag=teleportMarker] ~123 ~456 ~789 scoreboard players tag @a[r=42] add toTeleport

Then teleport them to the second area:

/tp @a[tag=toTeleport] ~987 ~654 ~321

Then clean up:

/scoreboard players tag @a[tag=toTeleport] remove toTeleport

And when you're done teleporting people around, you can kill the marker amour stands again:

/kill @e[tag=teleportMarker]

Killing only one of the markers works best with whatever you use to place/activate the command blocks, since that already knows the coordinates of that command block and therefore also of the armour stand.


1.13

In 1.13 you don't need a marker entity, since there is now /execute positioned. So you can just put

execute positioned ~123 ~456 ~789 run tag @a[distance=..42] add toTeleport

into the command block instead of summoning an entity and then executing from it.

  • Thank you for your answer. I tested it out, and it does work, but it seems what it's testing is if the radius encompasses the first coordinates, then it teleports. i.e. if ~-5 ~ ~ are the first coordinates and if r>=5, it doesn't matter if people are standing on the specific coordinates, they will be teleported from anywhere. Ideally I'd only like people standing on the coordinates to be teleported. – Andrew Nov 8 at 16:49
  • I don't really know what you mean. You can select only one block of range with r=1. Everybody inside will be selected. – Fabian Röling Nov 8 at 17:28
  • That's what I originally thought too, but I think the radius is relative to the command block, not relative to the relative coordinates. I'm just reporting what I found from testing lol. – Andrew Nov 8 at 17:40
  • It shouldn't. That's one of the main purposes of /execute. – Fabian Röling Nov 8 at 17:50
  • You can try it if you don't believe me. I first tried ~-5 ~ ~ with r=1 and it didn't work, but for some reason r>=5 does. – Andrew Nov 8 at 20:51

Your Answer

Andrew is a new contributor. Be nice, and check out our Code of Conduct.
 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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