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

my human.HealthChanged script loop ?

Asked by 5 years ago
Edited 5 years ago

So, after repeatedly trying to fix this problem on my own, I decided to ask you. Here's the thing: I tried to do a counter attack, like in "smash bros" for a fighting game. And it works, when I press the key, it do the animation , BUT, each time i take damage the counter attack loop.

debounce = true
if debounce == true and human.HealthChanged:Connect(function(health)
human.Health = human.MaxHealth
human:LoadAnimation(script.L):Play()
debounce = false
end)
then end

Any suggestion ? Thanks for reading, AKT_Arthas.

1 answer

Log in to vote
0
Answered by 5 years ago

The debouce should be inside the event. You may also need to manage the animation by stopping it if needed but that is something you would need to work out.

An example of a debounce.

local deb = false
human.HealthChanged:Connect(function(health)
    if deb then return end
    deb = true
    -- code
    deb = false
end)

Better still you often need to code this over and over so having a function to add a debounce for you may help.

local function addDeb(f)
    local deb = false
    return function(...)
        if deb then return end
        deb =true

        f(...) -- run function

        deb = false
    end
end

-- add debounce to function
human.HealthChanged:Connect(addDeb(function(health)
    -- code
end))

Hope this helps.

0
Thanks dude you saved me ! AKT_Arthas 2 — 5y
0
No problem. User#5423 17 — 5y
Ad

Answer this question