The health bar moves just fine when damaged but I want it to regenerate health every 3 seconds or so and I have tried so hard to make it like that but it still refuses to work.
wait(2)
game.Players.LocalPlayer.Character.Humanoid.Changed:connect(function()
local redzone = game.Players.LocalPlayer.Character.Humanoid.MaxHealth / 4
local yellowzone = game.Players.LocalPlayer.Character.Humanoid.MaxHealth / 2
if game.Players.LocalPlayer.Character.Humanoid.Health <= yellowzone then
script.Parent.BackgroundColor3 = Color3.new(255,255,0)
end
if game.Players.LocalPlayer.Character.Humanoid.Health > yellowzone then
local brickcolor = BrickColor.new("Lime green")
local color = brickcolor.Color
script.Parent.BackgroundColor3 = color
end
if game.Players.LocalPlayer.Character.Humanoid.Health <= redzone then
script.Parent.BackgroundColor3 = Color3.new(255,0,0)
end
if game.Players.LocalPlayer.Character.Humanoid.Health > redzone then
local brickcolor = BrickColor.new("Lime green")
local color = brickcolor.Color
script.Parent.BackgroundColor3 = color
end
script.Parent.Size = UDim2.new(game.Players.LocalPlayer.Character.Humanoid.Health / game.Players.LocalPlayer.Character.Humanoid.MaxHealth,0,0,20)
end)
-- Gradually regenerates the Humanoid's Health over time.
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.
--------------------------------------------------------------------------------
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
--------------------------------------------------------------------------------
while true do
while Humanoid.Health < Humanoid.MaxHealth do
local dt = wait(REGEN_STEP)
local dh = dt*REGEN_RATE*Humanoid.MaxHealth
Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
end
Humanoid.HealthChanged:Wait()
end
