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.

1 Answer 1


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.


The current barrier is very easy to break out of with a boat. ExpertCoder14 mentioned how to improve upon the command:

/execute as @a at @s unless entity @p[z=-369.5] run tp @s ~ ~ -369.5
  • 1
    Use @s[z=-368.5] instead of @p[...]. It will have the same end result, but it should be faster. Also, this can be very easily cheesable if you care about that, for example with a boat.
    – BunnyMerz
    Jul 23, 2022 at 18:15
  • Yes I realized yesterday how easy it is to escape. Is it possible to fix this by making a if not? Something like if not entity @s[z=-369.5] run tp @s ~ ~ -369.5
    – VinceKaj
    Jul 25, 2022 at 4:09
  • You could store the coordinates into a scoreboard and use the if score matches x..y and so something like if score @s Z matches -365..
    – BunnyMerz
    Jul 25, 2022 at 14:44
  • 1
    @VinceKaj if not is unless. /execute unless entity @s[...]
    – One 2 Many
    Aug 6, 2022 at 21:40

You must log in to answer this question.

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