The script above is better. It’s optimised and also shows the correct percentage of health. If say, you’re max health was say 150, it’d show it as 100% (assuming max health is also 150) instead of showing the literal number “150” like what your script did.
local Players = game:GetService("Players")
local Plr = Players.LocalPlayer::Player
local Label = script.Parent::TextLabel
local humanoid:Humanoid
local MaxHealth:number = 100
local function HealthChanged():()
Label.Text="Health: "..tostring((humanoid.Health/MaxHealth)*100//1).."%"
end
local function MaxHealthChanged():()
MaxHealth = humanoid.MaxHealth
end
local function CharAdded(char:Model):()
local h = char:WaitForChild("Humanoid")::Humanoid
humanoid = h
h:GetPropertyChangedSignal("MaxHealth"):Connect(MaxHealthChanged)
h.HealthChanged:Connect(HealthChanged)
HealthChanged()
end
Plr.CharacterAdded:Connect(CharAdded)
local char:Model? = Plr.Character
if char then CharAdded(char::Model) end
So, to make the health number round up, I used the math.ceil() function together with the HealthChanged event. That way, the text updates only when your health actually changes, no loops needed and it’s super efficient.
Here’s the part of the script that handles it.
local playerHumanoid = playerCharacter:WaitForChild("Humanoid")
local function updateVisualHealthDisplay()
local mxHlth = math.max(playerHumanoid.MaxHealth, 1) -- avoids dividing by zero
local hlthPerc = math.ceil((playerHumanoid.Health / mxHlth) * 100) -- rounds up
hlthPerc = math.clamp(hlthPerc, 0, 100) -- keeps it between 0 and 100
healthDisplayLabel.Text = string.format("%d%%", hlthPerc)
end
updateVisualHealthDisplay()
playerHumanoid.HealthChanged:Connect(updateVisualHealthDisplay)
playerHumanoid:GetPropertyChangedSignal("MaxHealth"):Connect(updateVisualHealthDisplay)
Quick Breakdown:
math.ceil() → rounds the number up (e.g. 33.3 → 34).
(humanoid.Health / max) * 100 → converts it into a percentage.
max.clamp() → keeps the value within 0-100.
HealthChanged and GetPropertyChangedSignal("MaxHealth") → make it update automatically whenever health changes or max health gets updated. So no loops are needed.
I was experimenting a bit with this by making a GUI that visually shows the health bar fill and change colors when healing or taking damage, just so you and I both see how this method behaves. As you’ll see this rounding method worked perfectly.
If this or any of the posts helped you, please consider marking one as a solution, so that we and other developers know the issue’s been resolved.