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

How do you make the Health round to nearest one? [closed]

Asked by 8 years ago
Edited 8 years ago

This question already has an answer here:

How do I round numbers in Lua? [Answered]

How do you make the Health round to nearest one in this script? This script is in an NPC. The health regeneration causes the health to be in a decimal place, but I want it to show only the whole number. In other words, I want the NPC's name to display as a whole number, but his health is still generating in decimals.

while true do
    script.Parent.Name = "Name Lv 1 " .. script.Parent.Humanoid.Health .. "/" .. script.Parent.Humanoid.MaxHealth
    wait()
end

Marked as Duplicate by User#5423, gskw, and Discern

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
einsteinK 145
8 years ago

You can use math.floor(value) to round it, cutting of the decimal part. If you want proper rounding, you can use this:

local function round(value)
    return math.floor(value+0.5)
end
print(round(1)) --> 1
print(round(1.2)) --> 1
print(round(1.4)) --> 1
print(round(1.6)) --> 2
print(round(1.8)) --> 2
print(round(2)) --> 2
0
How would I put in in my script? GatitosMansion 187 — 8y
0
"Name Lv 1"..math.floor(script.Parent.Humanoid.Health+0.5).."/"..script.Parent.Humanoid.MaxHealth einsteinK 145 — 8y
0
Thank you! It works :8 GatitosMansion 187 — 8y
Ad