Help with GUI needed!

Hey guys! I made an HP Bar and there aren’t any issues with it, but there is a slight problem. The HP text, it shows multiple numbers right after the HP! Like this for an example: “95.27479010249”. Is there any way to remove that?

local UI = script.Parent
local HPBar = UI["HP Bar"]
local KRBar = UI["KR Bar"]
local HPText = UI["Remaining Health"]
local Players = game.Players or game:GetService("Players")
local SetHPSize = false

print("Script active.")

local function GetHealth(Obj)
	if Players:GetPlayerFromCharacter(Obj) then
		local Character = Obj
		local Client = UI.Parent:FindFirstAncestorOfClass("Player")
		if Client and Character and Character:FindFirstChild("Humanoid", true) and Character.Name == Client.Name then
			local Humanoid = Character.Humanoid
			if Humanoid ~= nil then
				local HP = Humanoid.Health
				local MaxHP = Humanoid.MaxHealth
				if SetHPSize == false then
					SetHPSize = true
					HPBar.Size = UDim2.new(HP / MaxHP * 0.267, 0, 0.099, 0)
				    KRBar.Position = HPBar.Position+UDim2.new(0.234, 0, 0, 0)
					HPText.Text = tostring(math.floor(HP).."/"..math.floor(MaxHP))
					print("Health script working.")
				end
				local Signal = Humanoid:GetPropertyChangedSignal("Health")
				Signal:Connect(function()
					if Humanoid.Health < HP or Humanoid.Health > HP then
						local Hit = script.Hit:Clone()
						Hit.Parent = Humanoid.Parent
						Hit:Play()
						Hit.Ended:Connect(function()
							Hit:Remove()
						end)
						HP = Humanoid.Health
						MaxHP = Humanoid.MaxHealth
						HPBar.Size = UDim2.new(HP / MaxHP * 0.267, 0, 0.099, 0)
						HPText.Text = tostring(HP.."/"..MaxHP)
						print("Got hit. HP: "..HP)
					end
				end)
			end
		end
	end
end

game.Workspace.ChildAdded:Connect(function(Child)
	if Child.ClassName == "Model" then
		GetHealth(Child)
	end
end)

This is in a ServerScript. I tried everything I could to make it run on a LocalScript, but it just wouldn’t. If you know how to help me switch it over (which is optional just like helping me), I’d greatly appreciate it. Thanks, if you decide to help!

1 Like

Your issue is that you are using math.floor on the actual UI bar, but not applying that to the Text.

You can fix this by replacing the HPText.Text line with the following:
HPText.Text = tostring(math.floor(HP).."/"..math.floor(MaxHP))

note: I would recommend using math.round, as it is just more accurate. I have chosen math.floor because you’ve used it in this script.

2 Likes

You could just round up or down the HP value in your script so that you don’t have a float / decimal value.

2 Likes

Thanks so muchhhhh. This worked very well! Thanks for your help. I really appreciate it. :grinning:

1 Like

no problem!
have a good day

charcharchar

1 Like

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