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
ikDebris
(ikDebris)
June 25, 2023, 9:30am
#2
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
ikDebris
(ikDebris)
June 25, 2023, 9:34am
#4
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
ikDebris
(ikDebris)
June 25, 2023, 9:35am
#6
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
ikDebris
(ikDebris)
June 25, 2023, 9:38am
#8
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
system
(system)
Closed
July 9, 2023, 9:41am
#10
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.