Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make this only show whole numbers?

Asked by 4 years ago

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

Log in to vote
1
Answered by
Elixcore 1337 Moderation Voter
4 years ago

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)

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. Trisodin529 89 — 4y
0
I like you. Elixcore 1337 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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
Alternatively I suggest using `math.floor` because string operations are not the fastest in the block. EpicMetatableMoment 1444 — 4y

Answer this question