Ad
Log in to vote
0

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

Asked by 1 month ago

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)
0
btw you should do task.wait(3) instead of wait(3) NGC4637 587 — 1mo

2 answers

Log in to vote
0
Answered by 1 month ago

dm me on discord Blue Duck#8344

0
Alright TechModel 88 — 1mo
Ad
Log in to vote
0
Answered by 1 month ago

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)

Answer this question