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

Can you help me with this health style script? Please! :(

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 your health has not reached 100% 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? In other words: If the player gets hurt when the Health recovery function is active then the function stops and after 1 seconds is active again and needs to wait again the 6 seconds. How can I express this?

This is the code only for the health recovering function:

local regening = false

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

    if Humanoid.Health > Humanoid.MaxHealth then
        Humanoid.Health = Humanoid.MaxHealth
    end
    regening = false
end
0
I tried using debounce but it stayed the same oby11omi 5 — 9y
0
Since you want the function to stop if the players hurt again (if I understand correctly), then you mind need to put an if statement into the while loop to return from the function if "hurt" (arbitrary name) is true. Something like "if hurt then return end" inside the while loop, then call the function the same way it was originally called. GoldenPhysics 474 — 9y
0
i answered your question check if I did it properly davness 376 — 9y

1 answer

Log in to vote
0
Answered by
davness 376 Moderation Voter
9 years ago

You just need an while loop to run that idea.

So, you need to create an LocalScript inside StarterPlayerScripts.

What do you need to do?

1. Clean the old script: You can use destroy function over the regening script, and using WaitForChild()

game.Players.LocalPlayer.Character:WaitForChild("Health"):Destroy() -- clear the old script

2. Request the player's humanoid: You can acess it using LocalPlayer.Character

local Humanoid = game.Players.LocalPlayer.Character.Humanoid -- request the humanoid instance

3. Start the regening process:

while Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health > 0 do
    Humanoid.Health = Humanoid.Health + 0.08 * wait() * Humanoid.MaxHealth
    wait() -- crash prevent
end

4. Once you spawn, it will not work, because your Health is fully regened: So we need an while true loop inside our while loop

while true do
    while Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health > 0 do
        Humanoid.Health = Humanoid.Health + 0.08 * wait() * Humanoid.MaxHealth
        wait() -- crash prevent
    end
    wait() -- crash prevent
end

5. Prevent health flooding: To prevent a situation like your Health is bigger than your MaxHealth, just use an if statement and a simple comparison:

while true do
    while Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health > 0 do
        Humanoid.Health = Humanoid.Health + 0.08 * wait() * Humanoid.MaxHealth
        if Humanoid.Health > Humanoid.MaxHealth then
            Humanoid.Health = Humanoid.MaxHealth
        end
        wait() -- crash prevent
    end
    wait() -- crash prevent
end

So, for now, our script looks like this:

game.Players.LocalPlayer.Character:WaitForChild("Health"):Destroy() -- clear the old script

local Humanoid = game.Players.LocalPlayer.Character.Humanoid -- request the humanoid instance

while true do
    while Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health > 0 do
        Humanoid.Health = Humanoid.Health + 0.08 * wait() * Humanoid.MaxHealth
        if Humanoid.Health > Humanoid.MaxHealth then
            Humanoid.Health = Humanoid.MaxHealth
        end
        wait() -- crash prevent
    end
    wait() -- crash prevent
end

6. Regen every 6 seconds: Simple. Just add an wait(6)!

game.Players.LocalPlayer.Character:WaitForChild("Health"):Destroy() -- clear the old script

local Humanoid = game.Players.LocalPlayer.Character.Humanoid -- request the humanoid instance

while true do
    while Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health > 0 do
        Humanoid.Health = Humanoid.Health + 0.08 * wait() * Humanoid.MaxHealth
        wait(6)
        if Humanoid.Health > Humanoid.MaxHealth then
            Humanoid.Health = Humanoid.MaxHealth
        end
        wait() -- crash prevent
    end
    wait() -- crash prevent
end

7. Block regen if hurt for 1 second: We need some locals and anif statement. This is the final script.

game.Players.LocalPlayer.Character:WaitForChild("Health"):Destroy()

local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

while true do
    local h = Humanoid.Health
    while Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health > 0 do
        local h = Humanoid.Health -- request health
        wait() -- give time to the player to get hurt
        if Humanoid.Health < h then
            print ("In Danger! Recovering process interrupted!!") -- just for check proposes
            wait (1)
        end
        wait(6)
    Humanoid.Health = Humanoid.Health + 0.08 * wait() * Humanoid.MaxHealth
    if  Humanoid.Health > Humanoid.MaxHealth then
        Humanoid.Health = Humanoid.MaxHealth
    end
        wait() -- crash prevent
    end
    wait() -- crash prevent
end

PS: You won't get 1 second penalty if you get hurt and you were fully regened, but you will get it while regening.

0
Yes, well, I want like to restart again the function if the player gets hurt during recovery action oby11omi 5 — 9y
Ad

Answer this question