So I've been working on a custom health regeneration script for my Terraria-like game, and i tried to make it so when your not taking damage, a timer will start, and when it ends you'll regen hp. (faster if your standing still). But its only outputs "cant regen until not taking damage" once. Please help.
-- Gradually regenerates the Humanoid's Health over time, but this time like in Terraria.
local waitt = 1 -- the time between each regen
local regen = 1 -- how much you regen
local timer = 1.5 -- how long after not taking any damage regen will start
local canregen = false
--------------------------------------------------------------------------------
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
--------------------------------------------------------------------------------
while true do
if Humanoid.HealthChanged then
print "cant regen until not taking damage"
timer = 1.5
canregen = false
else
if timer < 0 then
canregen = true
print "able to regen"
return
else
timer = timer-0.1
canregen = false
print ("cant regen, timer is at ".. timer)
wait(0.1)
end
end
while Humanoid.Health < Humanoid.MaxHealth and canregen do
if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then waitt = 0.5 else waitt = 1 end
Humanoid.Health = Humanoid.Health + regen
wait(waitt)
end
Humanoid.HealthChanged:Wait()
end

1 comment
parenthesis arent Needed for waitforchild or findfirstchild