I'm having issues with my BuilderTool script

I’ve been trying for a few days now to create a builder tool. It mostly functions as intended.
When I create a new part using my tool, it places correctly on the grid however if I try to place a part off of another part, it clips into its side.

How can I avoid that and force the part to “offset” itself in such a way that it places next to it instead of partly inside of it?
Here are some examples of how the tool works and how I want it to work:


The intended result is in the background

Here’s the part of the code that handles part creation:

		if not newpart then
			newPart()
		elseif newpart then
			if canplace then
				local sentpart = {}
				sentpart.Size = newpart.Size
				sentpart.Position = newpart.Position
				sentpart.Anchored = newpart.Anchored
				sentpart.CanCollide = newpart.CanCollide
				sentpart.Orientation = newpart.Orientation
				game.ReplicatedStorage.BuilderTool:FireServer("New",sentpart)
				newpart:Destroy()
				newpart = nil
			end
		end

and here’s what handles the canplace condition:

RS.RenderStepped:Connect(function()
	if func == 1 then
		if newpart then
			local mousepos = mouse.Hit.p
			newpart.Position = Vector3.new(math.floor(mousepos.X),math.floor(mousepos.Y)+newpart.Size.Y/2,math.floor(mousepos.Z))
			local parts = newpart:GetTouchingParts()
			if #parts > 0 then 
				canplace = false
				newpart.BrickColor = BrickColor.new("Bright red")
			else
				canplace = true
				newpart.BrickColor = BrickColor.new("Bright green")
			end
		end
	end
end)

What I thought I could do was create the offset using magnitude between the two parts but unfortunately upon further research it would only work with parts that are equal in size and parallel.

Edit: Is it possible that I’d have to create a virtual grid? Although I don’t think that would function well as you could move parts off-grid using the move tool and unanchor tool and I’d like to be able to snap on parts to those parts too. Ideally I’d like it to function like the copy wand from build games.

bump as I’m still struggling with the issue

You could cast a small ray in every normal direction (Up,Down, Left, Right, Front, Back), and then find the closest part, and snap the part onto that part based on the direction from the part.

That’s a good idea actually, I’ll keep you posted if it works.

Well you have already solved this issue in your script except your code for it only accounts for the y-axis. Ideally (I’m trying to find a efficient method) that can find which face of the part your mouse is hitting and then offset the position for that axis and take in account for the size of the part.

Yeah I know, the problem was finding which face its touching, but I think the other person who replied just solved that issue for me!

Maybe with the use of Mouse.TargetSurface you can define which surface of the part is pointed at and calculate the offset relative to this part.

The problem with this I believe is that I need to get the X and Z directions which you don’t get using Mouse.TargetSurface