Any way to figure out how to round a parts position to a whole number?

Hello! I need help with aligning blocks to the grid.

  1. What do you want to achieve? I want to make it so my building system aligns to the grid.

  2. What is the issue? I cannot figure out how to round the parts position to a whole number, making it align to the grid.

  3. What solutions have you tried so far? I have looked all over the Devforum.

If you can help, it will be greatly appreciated!

Use math.abs(your number).

Always check developer.roblox.com since this has most of the answers you need.

It is a Vector3, so math.abs() cannot do it. Also, I checked and it wouldn’t round a normal number, so this is all not working.

making something align to a grid can be done like so:

local GridSize = 2 --The space between spots on the grid, so objects should be 2 studs apart.
function AlignVector3(Position) --Takes in any vector3
   Position.X = math.floor(Position.X/GridSize)*GridSize --If Position.X == 3, then 3/2 = 1.5, and 1.5 floored (rounded down) = 1, then we multiply it by the gridsize to space it out.
   Position.Y = math.floor(Position.Y/GridSize)*GridSize -- if it was 5, 5/2 = 2.5, rounded down to 2, 2*2 = 4
   Position.Z = math.floor(Position.Z/GridSize)*GridSize --If it was 1, 1/2 = 0.5, rounded down to 0, 0*2 = 0.
   return Position --Would have gone from Vector3(3, 5, 1) to Vector3(2, 4, 0) [xyz]
end
--//For example
print(AlignVector3(11, 5, 3)

console

10, 4, 2

If you want I can do a further walkthrough of why this works, and if you have any questions just ask.

local vec3 = Vector3.new(2.4, 2.1, 2.3)
local newvec3 : Vector3 = {math.abs(vec3.x), math.abs(vec3.y), math.abs(vec3.z)}

Even without Luau typehinting Lua will do an implicit conversion from that table to a Vector3.

1 Like

math.abs turns negatives into positives, getting the absolute distance from 0.
That doesn’t have anything to do with rounding or grid allignment???

lol, sorry to both you and @TheCursedSpud, I meant math.round.
math.floor would reduce 3.99 to 3, so math.round would work better.
brain no worky.
But like others have said, a Vector3 can have each value rounded inside the Vector3.

1 Like