Detect when players cross Z coordinate 1.19



  • I've seen similar questions, but their commands seem to be outdated. Although I don't have much experience with location-based command blocks, so forgive me if I am wrong.

    I am trying to execute a command if a player crosses a certain z-coordinate.

    Something like: /execute if entity @a[z=-369] run say @a.

    I.e., if a player crosses into z=-369, execute the command block.



  • I figured it out.

    /execute as @a at @s if entity @p[z=-368.5] run tp @s ~ ~ -369.5

    This command triggers if a player crosses into z=-368.5 and teleports them to -369.5

    The as @a at @s means that the command is executed from the player's position, not the command block's.

    Edit

    The current barrier is very easy to break out of with a boat. https://gaming.stackexchange.com/users/250180/expertcoder14 mentioned how to improve upon the command:

    /execute as @a at @s unless entity @p[z=-369.5] run tp @s ~ ~ -369.5
    


Suggested Topics