I have a health script, if you get hurt, after 6 seconds your health will start recovering faster than the common one until it reaches 100% . Problem is that if you get hurt again and the recovering health function is now running it doesn't stop, it continues to regen your health quicky and it stops only when your health reaches 100%. Is there a way to stop the function if the player gets hurt again while health is regening so if the player gets hurt again he needs to wait the 6 seconds again before his health starts recovering?
This is the code only for the health recovering function:
local regening = false function RecoverH() if regening then return end regening = true wait(6) --These are the seconds it has to wait to start the function :P while Humanoid.Health < Humanoid.MaxHealth do local health = Humanoid.Health if health > 0 and health < Humanoid.MaxHealth then health = health + 1 * 0.3 Humanoid.Health = math.min(health,Humanoid.MaxHealth) end end if Humanoid.Health > Humanoid.MaxHealth then Humanoid.Health = Humanoid.MaxHealth end regening = false end
Why don't you simply use Debounce which stop an function from running to many times?If you don't know how to use Debounce then I'll explain it to you how it works using comments.
local regening = false Debounce = false--Store if a function has been fired or not in a Variable function RecoverH() if not Debounce--Is it not fired? Debounce = true--Marks it as fired, so that other handler don't execute if regening then return end regening = true wait(6) --These are the seconds it has to wait to start the function :P while Humanoid.Health < Humanoid.MaxHealth do local health = Humanoid.Health if health > 0 and health < Humanoid.MaxHealth then health = health + 1 * 0.3 Humanoid.Health = math.min(health,Humanoid.MaxHealth) end end if Humanoid.Health > Humanoid.MaxHealth then Humanoid.Health = Humanoid.MaxHealth end regening = false Debounce = false--Mark it as not fired, so other handler can execute again. end end
local regening = false Debounce = false function RecoverH() if not Debounce--Is it not fired? Debounce = true if regening then return end regening = true wait(6) --These are the seconds it has to wait to start the function :P while Humanoid.Health < Humanoid.MaxHealth do local health = Humanoid.Health if health > 0 and health < Humanoid.MaxHealth then health = health + 1 * 0.3 Humanoid.Health = math.min(health,Humanoid.MaxHealth) end end if Humanoid.Health > Humanoid.MaxHealth then Humanoid.Health = Humanoid.MaxHealth end regening = false Debounce = false end end