char = game.Players.LocalPlayer.Character hum = char:WaitForChild("Humanoid") wait(3) hum.Changed:connect(function(update) script.Parent.Inner.Size = UDim2.new(hum.Health/289, 0, 0, 25) if hum.Health <= 20 then script.Parent.Inner.BackgroundColor3 = Color3.new(255, 0, 0) elseif hum.Health <= 60 then script.Parent.Inner.BackgroundColor3 = Color3.new(255, 221, 0) elseif hum.Health > 60 then script.Parent.Inner.BackgroundColor3 = Color3.new(12, 255, 0) end end)
There's a few problems here.
First, the Character
is likely to not be loaded by the time this LocalScript runs. We have to wait for it.
Here's a cute way to do that:
local player = game.Players.LocalPlayer local char = player.Character or player.CharacterAdded:wait()
Next, Color3
takes an r
, g
, and b
that are in the range 0
to 1
, not in the range 0
to 255
.
You have to divide those parameters by 255.
You shouldn't have elseif
when you mean else
-- you want the last branch to always happen (and it will anyway) -- don't write something that means nothing.
You're also repeating yourself five times with script.Parent.Inner
. Why not make a variable?
local player = game.Players.LocalPlayer local char = player.Character or player.CharacterAdded:wait() local hum = char:WaitForChild("Humanoid") wait(3) local inner = script.Parent.Inner hum.Changed:connect(function(update) inner.Size = UDim2.new(hum.Health/289, 0, 0, 25) if hum.Health <= 20 then inner.BackgroundColor3 = Color3.new(15, 0, 0) elseif hum.Health <= 60 then inner.BackgroundColor3 = Color3.new(1, 221/255, 0) else inner.BackgroundColor3 = Color3.new(12/255, 1, 0) end end)
Where did 289
come from? If it's the humanoid's max-health (which would make sense) then you should probably just use that so that you can change the MaxHealth without having to update this script.