Building system is weird

I made a building system but it wont place correctly. I used :MoveTo() so it would stand at ground level, but I’m wondering if there is a way for the 2 walls pass through each other but not through the ground

image

image

I tried turning off collisions but then I cant get them back on, Is there anything I could do with CanQuery or CanTouch?

local PosX, PosY, PosZ
local Grid = 1
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Part = game.ReplicatedStorage.Wall

local function Snap(posy)
	PosX = math.floor(mouse.Hit.X / Grid + 0.5) * Grid -- Snaps
	PosY = 0
	PosZ = math.floor(mouse.Hit.Z / Grid + 0.5) * Grid -- Snaps
end

local function Move()
	Part.Parent = game.Workspace
	mouse.TargetFilter = Part
	Snap()
	Part:MoveTo(Vector3.new(PosX, PosY, PosZ))
end

mouse.Move:Connect(Move)

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		local rotation = CFrame.Angles(0, math.rad(90), 0)

		local modelCFrame = Part:GetPrimaryPartCFrame()

		Part:SetPrimaryPartCFrame(modelCFrame * rotation)
	end
end)

mouse.Button1Down:Connect(function()
	local newpos = Vector3.new(PosX, Part.Base.Position.Y - 1, PosZ)
	for i,v in pairs(Part:GetChildren()) do
		if v:IsA("Part") then
			v.CanCollide = false
		end
	end
	local newpart = Part:Clone()
	newpart.Parent = game.Workspace
	newpart:MoveTo(newpos)
	
	for i,v in pairs(Part:GetChildren()) do
		if v:IsA("Part") then
			v.CanCollide = true
		end
	end
	for i,v in pairs(newpart:GetChildren()) do
		if v:IsA("Part") then
			v.CanCollide = true
		end
	end
end)


I think it’s because there’s no semicolon after the PosZ local declaration, so the local Grid declaration is being interpreted as a parameter for the Snap function.

So I would do this?

local function Snap(posy)
	PosX = math.floor(mouse.Hit.X / Grid + 0.5) * Grid -- Snaps
	PosY = 0
	PosZ = math.floor(mouse.Hit.Z / Grid + 0.5) * Grid;-- Snaps
end
2 Likes

I would suggest using a ‘cframing’ method of placement with Instance:PivotTo() , primarily because you don’t have to worry about any collisions with other things.
However, because there is no collision with the ground, you need to fire a ray (raycasting) either from your camera to the position the mouse is pointing, or from the sky, to the position you are placing the item, to detect where the ground is.

https://create.roblox.com/docs/mechanics/raycasting

Also, if your ground is a constant height (flat ground) you can use cframes but keep the Y at a constant level

If I turn collisions off that would work, but you can then walk through the walls. Is there anyway to make the collisions for the walls be off, but when you walk into a wall you wont pass through? Like only the walls have collisions ONLY with each other and nothing else?

The problem is with the MoveTo, you’re going to want to take SelDraken’s advice and do pivot to, because what the MoveTo does is it makes it so the model moves to the top of whatever point you put it at.

Thanks, I used raycasting for the y value and removed the :MoveTo