Placement system - Stacking (building on each other) does make parts fly to the sky

I am trying to make an placement system but I want to parts to stack on each other, I can’t. I tried Model.PrimaryPart.Position.Y + Model.PrimaryPart.Size.Y / 2 + Model.PrimaryPart.Size.Y / 2 method and it made my block fly instead of stacking on another block.

Here is my script for placement:

			local PosX
			local PosY
			local PosZ
				
			local Model = (ItemsList:WaitForChild("Background"):WaitForChild("Configuration"):WaitForChild("CurrentItem").Value):Clone()
			Model.Parent = workspace
				
			local GridSize = 2
				
			local function Snap()
				PosX = math.floor(Mouse.Hit.X / GridSize + 0.5) * GridSize
				PosY = Model.PrimaryPart.Position.Y + Model.PrimaryPart.Size.Y / 2 + Model.PrimaryPart.Size.Y / 2
				PosZ = math.floor(Mouse.Hit.Z / GridSize + 0.5) * GridSize
				--Model.PrimaryPart.Position.Y + Model.PrimaryPart.Size.Y / 2 + Model.PrimaryPart.Size.Y / 2
			end
			
			local function Movement()
				Mouse.TargetFilter = Model
				Snap()
				Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ))
			end
			
			Mouse.Button1Down:Connect(function()
				-- do on server instead of client (just dev note, ignore that note)
				local freshModel = Model:Clone()
				freshModel.Parent = workspace
				Mouse.TargetFilter = Model
				Snap()
				Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ))
			end)
				
			Mouse.Move:Connect(Movement)

There is nothing wrong with models

You need to replace model.PrimaryPart.Position.Y with math.floor(Mouse.Hit.Y + 0.5). The reason is that you are adding the part size in the Y dimension to the current position every time the mouse moves. This makes the position higher for the next time the mouse moves. It is never reset back to where the mouse is pointing.

2 Likes