How to make something happen after X seconds, but if it is interrupted it stops?

Hi, I would like to know how I can make stamina start charging back after 3 seconds, but if something happens, (I start running again), it stops waiting 3 seconds and has to wait 3 seconds again to start charging. How can I make this? Thanks in advance!

4 Likes

Use threads:

local function chargeStamina()
	task.wait(3)
	--increase stamina here
end

local thread = task.spawn(chargeStamina)

--on interruption:
task.cancel(thread) --cancel old timer
thread = task.spawn(chargeStamina) --start new one
2 Likes

Thank you so much! I had a little bit of trouble implementing cause I am not the best scripter, but it works, so thanks a bunch!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.