I need help with making parts snap together

So ive been trying to make this system where objects cant clip through eachother. I found this piece of code that raycasts everything and stuff but can someone please help me connect this to my code?

								if placingobject == true then
									mouse.TargetFilter = previewobject
									if previewobject:FindFirstChild("MainPart") then
										local MAX_PLACEMENT_DISTANCE = 200

										local ignoreList = {}
										-- The mouse ray is only one stud long so we need to make a new ray that is longer.
										local mouseRay = mouse.UnitRay
										local newRay = Ray.new(mouseRay.Origin, mouseRay.Unit*MAX_PLACEMENT_DISTANCE)
										local target, pos, norm = workspace:FindPartOnRayWithIgnoreList(newRay, ignoreList)
										local objectcframe = CFrame.new(math.round(mouse.Hit.Position.X/grid)*grid,math.round(mouse.Hit.Position.Y+previewobject.PrimaryPart.Size.Y/2), math.round(mouse.Hit.Position.Z/grid)*grid)
										local objectangles = CFrame.Angles(0, math.rad(rotationamount), 0)
										previewobject:SetPrimaryPartCFrame(objectcframe*objectangles)
									end
								else
									previewobject:Destroy()
								end
								if placingobject == false then
									script.Parent.Inventorybtn.Visible = true
									script.Parent.Rotation.Visible = false
								end
							end)
6 Likes