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

How to round number to nearest multiple of 15?

So there is this grid placing system but the only thing it misses is for it to snap to a unit which are 15 studs squared each. Since i only have the coordinates of the mouse.hit.p and those don't conform to the grid, i need to know how to round those to the nearest multiple of 15, positive or negative. thanks.

1 answer

✓ Accepted answer

Here's a simple formula:

I'm gonna use 15 here to fit your example.

--replace number with the variable you're trying to round
local roundednumber = (math.floor((number + 7.5)/15))*15

This works with all numbers. In case you decide to change to a different rounding factor, simply swap the 7.5 with half of the number you're rounding to, for instance if you're rounding to the nearest multiple of 40 you do:

--replace number with the variable you're trying to round
local roundednumber = (math.floor((number + 20)/40))*40

There's not much to it besides this really. Hope this helps!

I'm working on a grid placement system too btw, so if you can, please help me out :) https://scriptinghelpers.org/questions/117394/how-should-i-make-a-grid-building-system

1 comment

0

thanks!

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