Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
TechModel 118
3 years 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 602 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

dm me on discord Blue Duck#8344

0
Alright TechModel 118 — 3y
Ad
Log in to vote
0
Answered by 3 years 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