Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this only show whole numbers?

This is for a on-screen health bar. Sometimes, when its auto-healing, it shows something like, 87.39293478289901283/99 health. Is there any way to stop that?

repeat wait() until game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

while wait() do
	local Player = game.Players.LocalPlayer
	local Character = Player.Character
	local Humanoid = Character:WaitForChild("Humanoid")
	script.Parent.Size = UDim2.new(Humanoid.Health/Humanoid.MaxHealth,0,1,0)
	script.Parent.Parent.Version.Text = Humanoid.Health.."/"..Humanoid.MaxHealth
end

If there's anything I can do, please inform me. Thanks!

2 answers

✓ Accepted answer

Hey, quite a simple fix. you can use math.floor, or math.ceil on the number in order to make it go to the specified whole value (math.floor will keep the current integer and math.ceil will make the number go to its next integer)

return math.floor(10.1) --> 10
return math.ceil(10.1) --> 11

Additionally, if you want to round the number to its closest value you can just use

math.floor(number + 0.5)

(thanks Aleph)

2 comments

1

Yeah I just learned that from a yt video after seeing EpicMetatableMoment's post. I'll mark this as the answer, though, if anyone else stumbles upon this post.

0

I like you.

I had this question about a year ago when I was working on a project. What you need is to learn to format strings. https://developer.roblox.com/en-us/articles/Format-String

This is what you will probably be using.

("%u"):format("Decimal here")

I'm not well versed in string formatting but I think this is what you may use.

1 comment

1

Alternatively I suggest using math.floor because string operations are not the fastest in the block.

This discussion is preserved as it was. New answers, comments, and votes are closed.