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

How do you make the Health round to nearest one?

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

1 comment

1 answer

✓ Accepted answer

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

3 comments

0

How would I put in in my script?

0

"Name Lv 1"..math.floor(script.Parent.Humanoid.Health+0.5).."/"..script.Parent.Humanoid.MaxHealth

0

Thank you! It works :8

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