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
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.