How do I teleport a player when a mob gets close?

Solution 1:

Since you did not provide details to where you are going to be teleported, I will provide some examples.

Baseline command:

/execute as @a at @s if entity @s[distance=0..1, type=zombie] run tp @s x y z

Assume your "safe house" is at coordinates 0 64 0. Replace x y z with 0 64 0, so the player will be teleported to 0 64 0 if a zombie gets too close to a player.

You can also use tilde and caret notation. More information about that can be found here. To teleport the player 10 blocks forward, use ^ ^ ^10 instead of x y z.

I hope this helps. Comment down below if there are any issues with this solution.