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

How do I have it round the number on this HealthGui?

Asked by 9 years ago

So, I have this HealthGui, it works fine and on line 5 I have it put this TextLabel's text to the players health. But it gives you the exact digit like for example 90.09248 I would like it to round that number down to 90, or round whatever number it is up or down, but the problem is I don't know how.

Script:

local char = script.Parent.Parent.Parent.Character
hum = char.Humanoid
hum.Changed:connect(function()
script.Parent.Frame.HealthGui.Frame.Size = UDim2.new(hum.Health/hum.MaxHealth,0,1,0)
script.Parent.Frame.Text.TextLabel.Text = ""..hum.Health.."/"..hum.MaxHealth..""
if hum.Health <= hum.MaxHealth*0.15 then
script.Parent.Frame.HealthGui.Frame.BackgroundColor3 = Color3.new(150/255,0,0)
else
   script.Parent.Frame.HealthGui.Frame.BackgroundColor3 = Color3.new(0,220/255,0)
    end
end)



1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Lua provides a math.floor (floor) function and math.ceil (ceiling) function.

The "floor" of x is x without its fractional part. E.g.,

math.floor( 1.1 ) -- 1
math.floor( 1.0 ) -- 1
math.floor( 1.9 ) -- 1
math.floor( 1 ) -- 1

math.floor( -1.5 ) -- -2
math.floor(-2) -- -2

So, this isn't quite rounding (see the 1.9 example). The solution is to just add 0.5 to the value before flooring it:

math.floor( x + 0.5 )

--[[
-3  -3
-3  -2.75
-2  -2.5
-2  -2.25
-2  -2
-2  -1.75
-1  -1.5
-1  -1.25
-1  -1
-1  -0.75
0   -0.5
0   -0.25
0   0
0   0.25
1   0.5
1   0.75
1   1
1   1.25
2   1.5
2   1.75
2   2
2   2.25
3   2.5
3   2.75
3   3
]]
0
Thanks for clearing this up, I basically understand what you are saying about math.floor and stuff. I am just very noobish,.where would I put these things . It would be awesome if you can edit the script with the edits in it, ggggyourface23 63 — 9y
2
Think about it. What are you trying to round? The humanoid's Health value, right? Merely 2122 — 9y
Ad

Answer this question