[SOLVED] Custom Health Bar gives me a long String of decimals

I’ve made my own custom health bar, but when the player takes damage and starts to regenerate, it gives me a number with a very long string of decimals at the end. Is there an easy way to fix this, by just giving me a whole number with no other numbers at the end? Or do I have to use math.round?

Here is some of the code:

local maxHealth = human.maxHealth
local health = human.Health

HealthBar:TweenSize(UDim2.new(health/maxHealth, 0, HealthBar.Size.Y.Scale, 0))

HealthText.Text = health .. "/" .. maxHealth

human.HealthChanged:Connect(function(currentHealth)
	local maxHealth = human.maxHealth
	
	HealthBar:TweenSize(UDim2.new(currentHealth/maxHealth, 0, HealthBar.Size.Y.Scale, 0))
	
	HealthText.Text = currentHealth .. "/" .. maxHealth
	
end)
1 Like

To round the values to the closest number, you can use the math.round() function in Lua. Here’s how you can modify your script to include rounding:

local maxHealth = human.maxHealth
local health = human.Health

HealthBar:TweenSize(UDim2.new(math.round(health/maxHealth), 0, HealthBar.Size.Y.Scale, 0))

HealthText.Text = health .. "/" .. maxHealth

human.HealthChanged:Connect(function(currentHealth)
	local maxHealth = human.maxHealth
	
	HealthBar:TweenSize(UDim2.new(math.round(currentHealth/maxHealth), 0, HealthBar.Size.Y.Scale, 0))
	
	HealthText.Text = currentHealth .. "/" .. maxHealth
	
end)

By wrapping the division health/maxHealth and currentHealth/maxHealth with math.round(), the result will be rounded to the nearest whole number.

Is that everything? Do I just use math.round

That should be everything you need, just replace your script with the one I sent you.

Still not rounding for me. I copied the exact script

Can you show me the result and any errors?

No errors, it happens when the player starts to regenerate their health. Now the red bar behind isn’t appearing

Try this one…

local maxHealth = human.maxHealth
local health = human.Health

HealthBar:TweenSize(UDim2.new(health/maxHealth, 0, HealthBar.Size.Y.Scale, 0))

HealthText.Text = math.round(health) .. "/" .. math.round(maxHealth)

human.HealthChanged:Connect(function(currentHealth)
	local maxHealth = human.maxHealth
	
	HealthBar:TweenSize(UDim2.new(currentHealth/maxHealth, 0, HealthBar.Size.Y.Scale, 0))
	
	HealthText.Text = math.round(currentHealth) .. "/" .. math.round(maxHealth)
	
end)
1 Like

Thank you, works perfectly with the tweening too

1 Like

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