Why is this healthbar giving this error?

Achieve

I’m attempting to make this health bar script not error or break at all.

The Issue

I get an error of image everytime I try to fix this script.
It tends to not retrieve the data of the health properly when it breaks.

Note

I did not make this script, as much as I wish I could say I made this… I didn’t and that’s why i’m bringing it here. It’s a free health bar that errors often.

The Script

function _G.round(n)
return n - math.floor(n) >= 0.5 and math.ceil(n) or n - math.floor(n) < 0.5 and math.floor(n)
end

wait(0.5)
local char = game.Players.LocalPlayer.Character
hum = char.Humanoid
Digit = script.Parent.Holder.HealthbarHolder.Digits
Round = (_G.round(hum.Health))
hum.Changed:connect(function()
script.Parent.Holder.HealthbarHolder.Healthbar.Size = UDim2.new(hum.Health/hum.MaxHealth,0,0.65,0)
Round = (_G.round(hum.Health))
Digit.Text = (Round.."%")
if hum.Health <= hum.MaxHealth*0.20 then
script.Parent.Holder.HealthbarHolder.Healthbar.BackgroundColor3 = Color3.new(255,0,0)
else
script.Parent.Holder.HealthbarHolder.Healthbar.BackgroundColor3 = Color3.new(0, 255, 0)
end
end)

Thank you to anyone who reads and attempts to help me solve this issue.

1 Like

Try defining character and humanoid this way

local players = game:GetService("Players")
local player = players.LocalPlayer

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

Edit: I’ve noticed you were trying to use values that go all the way to 255 in Color3.new()

Either divide each value (r, g and b) by 255 or use Color3.fromRGB()

1 Like

@rokec123 answers your question but I thought I would encourage you to NOT use _G and instead use module scripts/bindables if you need to share variables across scripts.

1 Like

Oh no, this only goes through one script and with this… I didn’t make this health bar as I stated. I don’t understand most of what this person did for this health bar.

I state this as how he scripted it is the exact opposite way I would of scripted it.

Thank you though!

1 Like