Health shows decimal values when regenerating

No clue why it does this.
image
I tried making it round the health, but it didn’t work.
This is the localscript for the ui, by the way.

	local Humanoid = Player.Character:FindFirstChild("Humanoid")
	Main.HealthBar.Health.Text = "" .. tostring(math.round(Humanoid.Health)) .. " | " .. Humanoid.MaxHealth
	Humanoid.HealthChanged:Connect(function(dmg)
		Main.HealthBar.Health.Text = "" .. Humanoid.Health .. " | " .. Humanoid.MaxHealth
		Main.HealthBar.Bar.Size = UDim2.new(dmg / Humanoid.MaxHealth, 0, 1, 0)
	end)
1 Like

I believe this is an encounter with the “Floating Point” error. Here is some information on it.

1 Like

i think you forgot to floor the humanoid.health in the healthchanged event

2 Likes

The code seems to be meant to update a health bar based on changes in a player’s health.

Here’s a modified version of the code with some assumptions:

local Player = game.Players.LocalPlayer
local Main = -- Reference to your main user interface

local function UpdateHealthBar()
    local Humanoid = Player.Character and Player.Character:FindFirstChild("Humanoid")
    if Humanoid then
        Main.HealthBar.Health.Text = tostring(math.round(Humanoid.Health)) .. " | " .. Humanoid.MaxHealth
        Main.HealthBar.Bar.Size = UDim2.new(Humanoid.Health / Humanoid.MaxHealth, 0, 1, 0)
    end
end

local function HealthChanged(dmg)
    UpdateHealthBar()
end

Player.CharacterAdded:Connect(function(character)
    local Humanoid = character:WaitForChild("Humanoid")
    Humanoid.HealthChanged:Connect(HealthChanged)
    UpdateHealthBar()
end)

it’s just an assumption
I hope it has helped you

1 Like

I tried math.floor, round, and ceil. All same results.

1 Like

To add to the responses above, looking into math.floor() should point you into the right direction.

Note that math.floor() rounds decimal values down regardless of how they close to are to the next whole number. Depending on what you’re intending to do, math.ceil() can help you yield opposite results, i.e. rounding up to the next whole number.

1 Like

can you show your current code?

1 Like
local Humanoid = Player.Character:FindFirstChild("Humanoid")
Main.HealthBar.Health.Text = "" .. tostring(math.floor(Humanoid.Health)) .. " | " .. Humanoid.MaxHealth
Humanoid.HealthChanged:Connect(function(health)
	health = math.floor(health)
	Main.HealthBar.Health.Text = "" .. health .. " | " .. Humanoid.MaxHealth
	Main.HealthBar.Bar.Size = UDim2.new(health/ Humanoid.MaxHealth, 0, 1, 0)
end)
3 Likes

I’ve tried rounding, flooring, and ceiling, and all have came out with the same result of not working.

1 Like

This is all inside an individual script.

------------------------------------------------------------
StarterGui = game:GetService("StarterGui")
UI = script.Parent
Main = UI.Main
Player = game.Players.LocalPlayer
leaderstats = Player:WaitForChild("leaderstats")
------------------------------------------------------------
wait()
--hp bar
local Humanoid = Player.Character:FindFirstChild("Humanoid")
math.ceil(Humanoid.Health)
	Main.HealthBar.Health.Text = "" .. Humanoid.Health .. " | " .. Humanoid.MaxHealth
	Humanoid.HealthChanged:Connect(function(dmg)
		Main.HealthBar.Health.Text = "" .. Humanoid.Health .. " | " .. Humanoid.MaxHealth
		Main.HealthBar.Bar.Size = UDim2.new(dmg / Humanoid.MaxHealth, 0, 1, 0)
end)
1 Like
StarterGui = game:GetService("StarterGui")
UI = script.Parent
Main = UI.Main
Player = game.Players.LocalPlayer
leaderstats = Player:WaitForChild("leaderstats")
------------------------------------------------------------
wait()
--hp bar
local Humanoid = Player.Character:FindFirstChild("Humanoid")
	Main.HealthBar.Health.Text = "" .. math.floor(humanoid.Health) .. " | " .. Humanoid.MaxHealth
	Humanoid.HealthChanged:Connect(function(dmg)
		Main.HealthBar.Health.Text = "" .. math.floor(humanoid.Health) .. " | " .. Humanoid.MaxHealth
		Main.HealthBar.Bar.Size = UDim2.new(dmg / Humanoid.MaxHealth, 0, 1, 0)
end)

you forgot to do what I suggested above

1 Like

Okay, thank you. this worked. :slightly_smiling_face:

2 Likes

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