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

Can someone help me with this Health function? Pleace! :(

Asked by 9 years ago

I have a health script, if you get hurt, after 6 seconds your health will start recovering faster than the common one until it reaches 100% . Problem is that if you get hurt again and the recovering health function is now running it doesn't stop, it continues to regen your health quicky and it stops only when your health reaches 100%. Is there a way to stop the function if the player gets hurt again while health is regening so if the player gets hurt again he needs to wait the 6 seconds again before his health starts recovering?

This is the code only for the health recovering function:

local regening = false

function RecoverH()
    if regening then return end
    regening = true
    wait(6) --These are the seconds it has to wait to start the function :P
    while Humanoid.Health < Humanoid.MaxHealth do
        local health = Humanoid.Health
        if health > 0 and health < Humanoid.MaxHealth then
            health = health + 1 * 0.3 
            Humanoid.Health = math.min(health,Humanoid.MaxHealth)
        end
    end

    if Humanoid.Health > Humanoid.MaxHealth then
        Humanoid.Health = Humanoid.MaxHealth
    end
    regening = false
end

2 answers

Log in to vote
0
Answered by 9 years ago

Why don't you simply use Debounce which stop an function from running to many times?If you don't know how to use Debounce then I'll explain it to you how it works using comments.

local regening = false
Debounce = false--Store if a function has been fired or not in a Variable
function RecoverH()
  if not Debounce--Is it not fired?
Debounce = true--Marks it as fired, so that other handler don't execute 
 if regening then return end
    regening = true
    wait(6) --These are the seconds it has to wait to start the function :P
    while Humanoid.Health < Humanoid.MaxHealth do
        local health = Humanoid.Health
        if health > 0 and health < Humanoid.MaxHealth then
            health = health + 1 * 0.3 
            Humanoid.Health = math.min(health,Humanoid.MaxHealth)
        end
    end

    if Humanoid.Health > Humanoid.MaxHealth then
        Humanoid.Health = Humanoid.MaxHealth
    end
    regening = false
Debounce = false--Mark it as not fired, so other handler can execute again.
end
end
0
Thanks for answering my question it's my second time asking something here. I tried it the way you put it but the problem continues, recovery doesn't stops until health reaches 100% oby11omi 5 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
local regening = false
Debounce = false
function RecoverH()
  if not Debounce--Is it not fired?
Debounce = true
 if regening then return end
    regening = true
    wait(6) --These are the seconds it has to wait to start the function :P
    while Humanoid.Health < Humanoid.MaxHealth do
        local health = Humanoid.Health
        if health > 0 and health < Humanoid.MaxHealth then
            health = health + 1 * 0.3 
            Humanoid.Health = math.min(health,Humanoid.MaxHealth)
        end
    end

    if Humanoid.Health > Humanoid.MaxHealth then
        Humanoid.Health = Humanoid.MaxHealth
    end
    regening = false
Debounce = false
end
end

Answer this question