How to round health number up in GUI?


local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

while wait() do

	script.Parent.Text = "Health: "..humanoid.Health.."%"

end

Hi all, this is a script to display the players health, but unfortunately it shows a lot of decimals, how do I round the number?

2 Likes

Use math.round(), math.floor() or math.ceil().
Next time, please, try googling your question before creating a topic

3 Likes

Are you wanting to simply remove all decimals or just round to a specific decimal?

script.Parent.Text = ("Health: %d%"):format(humanoid.Health)

or

script.Parent.Text = `Health: {math.floor(humanoid.Health)}%` 
1 Like
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

script.Parent.Text = "Health: "..math.round(humanoid.Health/humanoid.MaxHealth*100).."%"
humanoid.HealthChanged:Connnect(function()
	script.Parent.Text = "Health: "..math.round(humanoid.Health/humanoid.MaxHealth*100).."%"
end)

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.

To round numbers you just do:

local Number = 20.5285252
local Decimal = 2

local Rounded = math.round(Number * 10 ^ Decimal) / 10 ^ Decimal
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

i recommend using math dot ceil instead of Math.floor because then if hp is like 0.9 it would display 0

Hey!

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. :slight_smile:

[Visual Player Healing till a certain amount]
playerVisualHealTill


[Visual automatically healing by Roblox]
damagaAutoHealPlayerVisuals

But that breaks dramatic effect.
It would be badass to say, “Lmao, I killed you when I was at 0 hp”

i recommend using math dot round instead of Math.ceil because then if hp is like 0.3 it would display 1

math.round rounds to zero at a decimal lower than 0.5

so what, at 0.5 you are dead, better than displaying u have more hp

u only get sent to dead state at hp <=0

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.