Health Bar Numbers Help

This healthbar script works and displays a number on the bar. 100/100. However when I take damage, it drops down, then when it regenerates it adds a decimal point and 12 more numbers. Example after falling. 82/100. Then the first regen of health makes it 82.157283927239/100. It continues on that until it hits 100/100 again.

Anyone know how to get rid of the decimal and extra numbers during regeneration?

local bar = script.Parent:WaitForChild("Bar")
local hpText = script.Parent:WaitForChild("HP")
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local connection_health
local connection_max_health
local character = player.Character

local function update()
	local humanoid = character:WaitForChild("Humanoid")
	bar:TweenSize(UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quint, 1, true)
	hpText.Text = humanoid.Health .. "/" .. humanoid.MaxHealth .. " HP"
end

local function setConnections()
	local humanoid = character:WaitForChild("Humanoid")
	connection_health = humanoid:GetPropertyChangedSignal("Health"):Connect(update)
	connection_max_health = humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)
	update()
end

player.CharacterAdded:Connect(function(char)
	character = char
	if connection_health then connection_health:Disconnect() end
	if connection_max_health then connection_max_health:Disconnect() end
	setConnections()
end)

setConnections()

Use math.round() to round the number to the nearest integer.

2 Likes
local bar = script.Parent:WaitForChild("Bar")
local hpText = script.Parent:WaitForChild("HP")
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local connection_health
local connection_max_health
local character = player.Character

local function update()
	local humanoid = character:WaitForChild("Humanoid")
	bar:TweenSize(UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quint, 1, true)
	hpText.Text = math.round(humanoid.Health) .. "/" .. humanoid.MaxHealth .. " HP"
end

local function setConnections()
	local humanoid = character:WaitForChild("Humanoid")
	connection_health = humanoid:GetPropertyChangedSignal("Health"):Connect(update)
	connection_max_health = humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)
	update()
end

player.CharacterAdded:Connect(function(char)
	character = char
	if connection_health then connection_health:Disconnect() end
	if connection_max_health then connection_max_health:Disconnect() end
	setConnections()
end)

setConnections()
1 Like

You could use math.floor, it basically deletes the integer

Example: 12.1230129033193090939
You use math.floor on it, it will rounds to 12

Another example: 86.1
math.floor will make it 86

So how do you put it into Lua? just do: math.floor(TheNumberYouWant)

In your situation, you should round the humanoid.Health on the update() function so it’s math.floor(humanoid.Health)

Replace the update() function to:

local function update()
	local humanoid = character:WaitForChild("Humanoid")
	bar:TweenSize(UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quint, 1, true)
	hpText.Text = math.floor(humanoid.Health) .. "/" .. humanoid.MaxHealth .. " HP"
end

Sorry very rushed I don’t have a proper explanation for this but I hope you understand
ok bye :wave:

Also math.round is definitely a great choice too! Depends on what you want

1 Like

Thanks for those quick solutions.