I have a health bar gui that I would like some work done on. I’ve tried looking into it and fixing it but can’t find out how. When the player is hit and is regenerating the textlabel inside the gui will show decimal points with it. How could I fix this?
Example:

Code:
--//Variables
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local healthGui = script.Parent
--//Health
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
local healthColor = Color3.fromRGB(255,0,0):Lerp(Color3.fromRGB(85,255,0),humanoid.Health/humanoid.MaxHealth)
local healthChange = humanoid.Health/humanoid.MaxHealth
healthGui.Health.Meter:TweenSize(UDim2.new(healthChange,0,1,0),"Out","Bounce",0.5)
healthGui.Health.Meter.BackgroundColor3 = healthColor
end)
3 Likes
I beleive you simply need to make it so the number that is showed is rounded up. You can either use math.ceil or math.floor. Math.ceil returns the smallest integer larger than or equal to x and math.floor returns the largest integer smaller than or equal to x.
I tried to use math.floor
but that didn’t work. What am I doing wrong?
--//Variables
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local healthGui = script.Parent
local TL = script.Parent.Health.TextLabel
local healthValue = humanoid.Health
--//Health
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
local healthColor = Color3.fromRGB(255,0,0):Lerp(Color3.fromRGB(85,255,0),humanoid.Health/humanoid.MaxHealth)
local healthChange = humanoid.Health/humanoid.MaxHealth
healthGui.Health.Meter:TweenSize(UDim2.new(healthChange,0,1,0),"Out","Bounce",0.5)
healthGui.Health.Meter.BackgroundColor3 = healthColor
TL.Text = math.floor(healthValue)
end)
1 Like
Try moving local healthValue = humanoid.Health
inside the function
Coming to your script, as @3MtnBros said,
--//Variables
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local healthGui = script.Parent
local TL = script.Parent.Health.TextLabel
local healthValue = humanoid.Health --Constant Value
--//Health
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
local healthColor = Color3.fromRGB(255,0,0):Lerp(Color3.fromRGB(85,255,0),humanoid.Health/humanoid.MaxHealth) --Here humanoid.Health is updated
local healthChange = humanoid.Health/humanoid.MaxHealth -- Here humanoid.Health is an updated value
healthGui.Health.Meter:TweenSize(UDim2.new(healthChange,0,1,0),"Out","Bounce",0.5)
healthGui.Health.Meter.BackgroundColor3 = healthColor
TL.Text = math.floor(healthValue) --But over here you are using the Constant Variable, The Health value is not updated since it is outside the function.
end)
So, in other words healthValue is not updated every time, try keeping it under the function so that it updates every time.
I’ve seen many people recommending using floor
. This probably isn’t the best approach as it restricts the freedom of your decimals and also makes it harder to edit, consider looking into string formatting
As for applying this to your own code
Lets assume we have a new variable called ‘HealthLabel’ which points to your text label
local HealthLabel = script.Parent.TextLabel
To apply a formatted string, we’re going to use the %01d formatting character which makes the string appear as a whole number, it’s worth noting that format strings can have raw text and appear fine
--the %s method simply acts as a insert variable here character
print(string.format("%s is cool", "007David007YT")
--outputs "007David007YT is cool"
What you need to do is, use string.format
and parse the health into it
HealthLabel.Text = string.format("%01d", HealthChange)
ps if you want decimals you can use the %.#f format where # is the amount of decimals you want outputed
1 Like
I mean you could use
string.sub(healthValue, 0, 3)
If you want it to cut off
2 Likes
TL.Text = math.floor(healthValue) --But over here you are using the Constant Variable, The Health value is not updated since it is outside the function.
you forgot tostring
1 Like
TL.Text =tostring( math.floor(healthValue))
This is redundant, numbers automatically get converted into strings if you do anything string related to them
2 Likes
I tried all your guys’s code and none of if worked.
Could you please post information if it didn’t work, then? Vague responses aren’t too helpful. If you encounter an issue, whether it’s the root issue (OP) or replying with attempted solutions that failed to work, please include as much information as you can.
Knowing specifically what you tried and how that attempt didn’t work is important to know, including if anything could be found in the console and if you tried to debug this yourself first. There are some here who copy-paste code and if it doesn’t work, report back without trying to make some minor changes first.
That being said, all the solutions above are valid and should work, but I’m not too fond of them and don’t know if they even work. Instead of all that, why not just floor the division itself? I can whip up a quick code sample of how I’d write this.
-- First off, LocalPlayer will always be available to LocalScripts. Second off,
-- if you use the `or PlayerAdded:Wait()` method, then your player would be
-- someone completely different if LocalPlayer was ever nil, which it won't be.
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local healthGui = script.Parent
-- Just so it's easier to change in the future
local LOW_COL = Color3.fromRGB(255, 0, 0)
local HIGH_COL = Color3.fromRGB(85, 255, 0)
-- HealthChanged is the canonical event for detecting changes to the
-- Humanoid's health.
humanoid.HealthChanged:Connect(function (newHealth)
local healthPercent = newHealth/Humanoid.MaxHealth
local healthRounded = math.floor(healthPercent)
local healthColor = LOW_COL:Lerp(HIGH_COL, healthPercent)
-- meter sets whatever
-- use healthRounded for the text
end)
As @FilteredDev was saying, if you’re solely wanting to get rid of the decimals in the text label.
TextLabel.Text = string.format("%2.2f", humanoid.Health)
would drop it down to two decimal places. However your script shown doesn’t seem to be editing the text itself. So if you could show us how you’re displaying the text itself we could give you a better answer.
I’m very sorry for writing without the amount of information wanted. I figured out that all of the scripts would have worked! I didn’t notice though because I had another script that would update the textlabel every second but it would now add decimals because before the script used to be:
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local Label = script.Parent
while wait(0.1)do
Label.Text = humanoid.Health
end
But now I added a line to it.
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local Label = script.Parent
while wait(0.1)do
Label.Text = humanoid.Health
Label.Text = string.format("%2.0f", humanoid.Health)
end
Thanks a lot for the help!