Grid system help

I need help making a block grid system that supports many different sizes ( a mix of 4s and 2s, depending on the block type ) and orientations ( tilting & rotating which i’ve already coded ). I’ve tried many things but none of them work. I just want a reliable function that takes in a Vector3 position and returns the suitable one. How can I do it?

Example :


function snapToGrid(position, size)
    local snappedX = math.round(position.X / size.X) * size.X
    local snappedY = math.round(position.Y / size.Y) * size.Y
    local snappedZ = math.round(position.Z / size.Z) * size.Z
    return, snappedY, snappedZ)

-- Example usage
local position =, 5.3, 8.1)
local size =, 5, 5)
local snappedPosition = snapToGrid(position, size)

print(snappedPosition) -- Output:, 5, 10)

This is from ChatGPT. It looks good to me but I didn’t try it.

I’ve tried ChatGPT many times before, but I couldn’t achieve this kind of grid system. I know a game that has it but I don’t know how they did it.

This is what happens by the way