Help on grid placement system

I am currently trying to develop a game that allows players to place down specific blocks and items onto a canvas (their plot). This stops people from building outside their plot, snapping items in a grid (2 studs in my case), and can build parts of different sizes (in multiples of the grid snap size), while building above the floor, as in you can place blocks on top of each over etc. This design would be very similar (if not almost the same) as Plane Crazy’s placement system.

After a lot of research, I still cannot seem to figure out how to work everything in relation to world and object spaces, rotations etc. The problem is the calculation of the CFrame of items.

I have looked up and tried a few different solutions, but they limit out rotations and CFrames, or don’t allow objects to be placed on top of one another.

Here is a snippet of position calculation I currently have for reference:

local function SnapPosition(x, y, z)
	return math.floor(x / step + 0.5) * step, math.floor(y / step + 0.5) * step, math.floor(z / step + 0.5) * step

local function CalcPlacementCFrame(model, rotation)
	local ground = p.Plot.Value.Ground
	local modelSize = model.PrimaryPart.Size
	local position = mouse.Hit.p
	local target = mouse.Target
	local surface = mouse.TargetSurface
	local x = position.X
	local y = position.Y
	local z = position.Z
	if target == ground then
		y = ground.Position.Y + ground.Size.Y / 2 + modelSize.Y / 2
	x, y, z = SnapPosition(x, y, z)
	return, y, z)

The experience looks like this when building: