Snap To Grid - grid misaligning when base is moved

Hi there,

I have a snap to grid system which works like this

local PlotOrigin = CFrame.new() * CFrame.fromAxisAngle(Vector3.new(0,1,0), PlayerGym.PlotRotation)

local Util = {}

local function round(x, m)
    return math.floor((x/m) + .5) * m
end

function Util.SnapToGrid(position, increment, objectSize)
    local relativePosition = PlotOrigin:vectorToObjectSpace(position)
    local relativeSize = PlotOrigin:vectorToObjectSpace(objectSize)
    local xOffset = (relativeSize.X/2) % increment
	local zOffset = (relativeSize.Z/2) % increment

    local newRelativePosition = Vector3.new(round(relativePosition.X, increment)+xOffset,
        position.Y, round(relativePosition.Z, increment)+zOffset)
    return PlotOrigin:vectorToWorldSpace(newRelativePosition)
end

And that works well, like this

The issue is when the player rejoins, the base they have is often different and so will be rotated differently. The parts are saved relative to the floor they’re placed on, meaning they’re moved from the grid.

The floor itself also changes size so I need a very flexible solution, not sure the best way to approach this problem. But basically everything should be in the same position relative to the front centre of the base part. Any help with this would be super appreciated

local PlotOrigin = CFrame.new() * CFrame.fromAxisAngle(Vector3.new(0,1,0), PlayerGym.PlotRotation)

isn’t this just a rotation at (0, 0, 0)? why not set the position too? like some arbitrary point on the base. then vectorToObjectSpace does the rest when it reloads?

why use math.floor when math.round would be better

return math.round(x / m) * m