Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to reset wait() once HealthChanged is fired multiple?

Hello! This is something I'm not sure what this is called or how to do fix this, but how this works is a gui that appears and dissapears when it is health changed. But seeing that as it continues to be damaged for more than 3 seconds, the gui starts flashing instead of resetting the 3 seconds before disabling the healthbar. How do I resolve this issue? Thanks.

local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")

Humanoid.HealthChanged:Connect(function()
		
	script.Parent.Parent.Parent.HealthBar.Enabled = true
	
	script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth)
	
	local pie = (Humanoid.Health / Humanoid.MaxHealth)
	
	script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0)
	
	wait(3) -- This part where I want to reset the wait if the function's called or fired again.
	
	script.Parent.Parent.Parent.HealthBar.Enabled = false

end)

1 comment

0

btw you should do task.wait(3) instead of wait(3)

2 answers

You can simply use a bool and if statements as a debounce. I'll use your script as an example of it.

local Humanoid = script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
local bool = false

Humanoid.HealthChanged:Connect(function()
   if not bool then
    bool = true
    script.Parent.Parent.Parent.HealthBar.Enabled = true

    script.Parent.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth)
    local pie = (Humanoid.Health / Humanoid.MaxHealth)

    script.Parent.Healthbar.Size = UDim2.new(pie, 0, 1, 0)
    script.Parent.Parent.Parent.HealthBar.Enabled = false
else
   task.wait(0.5)
   bool = false
end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.