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()
2. Request the player's humanoid: You can acess it using LocalPlayer.Character
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
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()
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()
end
wait()
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()
end
wait()
end
So, for now, our script looks like this:
game.Players.LocalPlayer.Character:WaitForChild("Health"):Destroy()
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
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()
end
wait()
end
6. Regen every 6 seconds: Simple. Just add an wait(6)!
game.Players.LocalPlayer.Character:WaitForChild("Health"):Destroy()
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
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()
end
wait()
end
7. Block regen if hurt for 1 second: We need some locals and an if 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
wait()
if Humanoid.Health < h then
print ("In Danger! Recovering process interrupted!!")
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()
end
wait()
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.
3 comments
I tried using debounce but it stayed the same
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.
i answered your question check if I did it properly