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

How do you restart a function before completing its contents?

Asked by 6 years ago

Hey guys, So I have created a simple custom healthbar Gui that turns red when the player is damaged and turns back to the normal color after 1 second. However, I have noticed that if the player is damaged more than once within one second, the wait(1) does not "restart." In other words, the healthbar does not turn back to normal color after 1 second after the last time it was damaged. For instance, if the player is damaged, and then damaged again after 1 second, the healthbar does not turn red for the second time the player is damaged because the wait(1) from before causes the healthbar to turn back to normal color. If someone actually understood everything I just said, I would really appreciate it if you could explain to me how to solve this. Thanks in advance

--Defined variables above 
humanoid.HealthChanged:connect(function(health)
    healthBackground.BackgroundColor3 = Color3.fromRGB(200,0,0)--Damaged color
    healthbar:TweenSize(UDim2.new(health/100,0,1,0), "Out", "Quint", 0.5, true) --Move healthbar
    wait(1)
    healthBackground.BackgroundColor3 = Color3.fromRGB(150,150,150) --Normal color
end)

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This make sure you weren't hurt again before it turns the health back to green:

--Defined variables above
local ounchdeboune = 0
humanoid.HealthChanged:connect(function(health)
    healthbar:TweenSize(UDim2.new(health/100,0,1,0), "Out", "Quint", 0.5, true) --Move healthbar
    if prevhealth > health then
        ounchdeboune = ounchdeboune + 1
        prevhealth = health
        healthBackground.BackgroundColor3 = Color3.fromRGB(200,0,0)--Damaged color
        wait(1)
        ounchdeboune = ounchdeboune - 1
        if ounchdeboune <= 0 then
            ounchdeboune = 0
             healthBackground.BackgroundColor3 = Color3.fromRGB(150,150,150) --Normal color
        end
    else
        prevhealth = health
    end
end)
0
Oh, I forgot about debounces! Thank you very much dpark19285 375 — 6y
Ad

Answer this question