Placement system not snapping to grid if the part size is not even

im working on a placement system that snaps into a 1x1 stud grid, but the problem is that it doesnt snap if the hitbox size X or Z is not even then it snaps the hitbox between the grids


i have tried to add half a stud if its not even but it didnt work well so im hoping for some help here.
here is the script if it may help

local module = {}

module.place = function(plr,item)
	local mouse = plr:GetMouse()
	local function snapToGrid(vector3,middle,model)
		local x = math.floor(vector3.X+0.5)
		local y = math.floor(vector3.Y+0.5)
		local z = math.floor(vector3.Z+0.5)
		model:MoveTo(middle+Vector3.new(-x,y + model.PrimaryPart.Size.Y/2,-z))
	end
	local hitbox = item.Parts.PrimaryPart
	local RS = game:GetService("RunService")
	local maxHeight = 250
	local plot
	for i,v in pairs(game.Workspace.Plots:GetChildren()) do
		if v.Owner.Value == plr.Name then
			plot = v
			for i,b in pairs(v.BaseParts:GetChildren()) do
				b.Grid.Transparency = 0
			end
		end
	end
	repeat
		local pos = plot.BaseParts.BasePart1.Position - mouse.Hit.p
		snapToGrid(pos,plot.BaseParts.BasePart1.Position,item.Parts)
		wait()
	until item == nil
end

return module

Did you find a solution to this because I am having the same problem you where having.

yeah i wrote a function which is like 300 lines long lul
however if you want to make a 2 dimensional placement system, it will be much easier. mine is a 3 dimensional placement system which means you can place 2 structures on top of each other, thats why the function got so long