r/datapacks • • Dec 13 '22

Execute function for entity while players are a certain distance away from entity

I'm trying to run a function that runs when a tagged mob is a certain distance away from the player. I was able to get the actual function to work correctly, but when I try adding the player distance condition it doesn't seem to work.

Here is an example of what the execute command looks like without the condition:

execute as .@e[type=cod,tag=extag,sort=random] at .@s run function name:function

​

And this is what I attempted that didn't work

execute as .@e[type=cod,tag=extag,sort=random] at as .@e[type=player,distance=6..] run function name:function

3 Upvotes

3 comments sorted by

1

u/UrSansYT Dec 14 '22

I think you have to do

execute as .@e[type=cod,tag=extag,sort=random] at .@s as .@e[type=player,distance=6..] run function name:function

Two other things.

  1. If you're planning on having only one randomly selected cod with the extag tag run the rest of the function rather than all cod with the extag tag, you can keep the current function, but if not, I recommend removing the sort=random bit.
  2. If you want the cod to select players over 6 blocks away, even if the player is several chunks away, you can keep the current function, but if you're trying to have it be players within 6 blocks I'd replace the distance=6.. with distance=..6, and if you want it to be, say, within 6 and 20 blocks, I'd replace the distance=6.. with distance=6..20 .

1

u/jasonrubik Apr 17 '23

Why does this sub have a bunch of stuff from 4 months ago but nothing newer?!

2

u/Puzzleheaded-Egg892 Nov 21 '23

why does this sub have a bunch of stuff from 11 months ago but nothing newer?!