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

How can I round the magnitude to the nearest whole?

local player = game.Players.LocalPlayer 
local v = workspace.RedZone

while true do
	wait(0.01)
	local mag = (v.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude 
	if mag < 10 then
		mag = "Inbound"
	end
	script.Parent.Text = mag
end


I currently have this script which gets the magnitude between a part and the player. The magnitude always pops up as 10.34323331232 or smth. How can I round the magnitude to the nearest whole?

2 answers

Using % operator. It gets the remainder of a quotient. For example, 15%7 = 1. To learn more on operators, click here.

if mag%1 >= 0.5 then -- If the remainder is greater or equals to 0.5
	mag = math.ceil(mag) -- Rounds up the value
	else
	mag = math.floor(mag) -- Rounds down the value

If you want it to round down or round up only, use

math.ceil() -- Rounds up

math.floor() -- Rounds down

I don't think there is a 'math.round()' function. Would be great if there were tho.

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