How to check for something every tick, but only run a command one time when the condition is met?
I'm making a Minecraft 1.16 datapack and I need to check if a player is in a certain location. I want to check for that condition every tick and then run a function when it is met.
I'm currently using this command: execute as @a[x = 10, y = 56, z = 42, dx = 8, dy = 10, dz = 0] run say Passed
The problem is that when the condition is met, the run say Passed
part also executes every tick, which is not what I want.
When the condition is met, I want that command to only run once, and not every tick, while still checking the condition every tick. But, I also want the command to be able to run again after a second or so.
Could I do this by checking the condition every tick, then running a function that has a timer that only executes a command if it was previously executed 1 or more seconds ago?
you could make a scoreboard that stores a timer, that counts down to 0 and once that 0 is hit, the command can be run again setup:
/scoreboard objectives add Timers dummy
so you'll run these functions each tick:
execute if score FunctionX Timers matches 1.. run scoreboard players remove FunctionX Timers 1
execute if score FunctionX Timers matches 0 run execute as @a[x = 10, y = 56, z = 42, dx = 8, dy = 10, dz = 0] run function <namespace>:passed
and in passed.mcfunction
scoreboard players add FunctionX Timers 20
say passed
this will make the command run only every 20 gameticks