Building keeps no-clipping near the top of other buildings

  1. I want to make a building mode in my building system where you can place walls glued to each other.

  2. The mode works fine until it reaches near the top of other buildings, when it does it starts no-clipping inside of them. It only doesn’t do that when my camera is lower than the building’s top, so I’m guessing it has to do something about the camRay.

How it should look like:
image

How it looks like:

image

Do note that it only no-clips when it’s near the top and my camera is higher than the block, if my camera is lower, then it doesn’t no-clip.

  1. I tried looking through the devforum and tried changing my code a ton of times to fix that, but none of that seemed to work. If I missed a devforum page that went through this, please link it to me.

This is my code:

		while equipped and eMode do
			RunService.Heartbeat:Wait()
			if (mouse.Hit.p - tool.Handle.Position).Magnitude <= 50 then
				sticky.BrickColor = BrickColor.new("Bright green")	
			else
				sticky.BrickColor = BrickColor.new("Really red")
			end
			local camRay = camera:ViewportPointToRay(mouse.X, mouse.Y)
			local param = RaycastParams.new()
			param.FilterDescendantsInstances = {mouse.Target}
			param.FilterType = Enum.RaycastFilterType.Whitelist
			local res = workspace:Raycast(camRay.Origin,camRay.Direction*600,param)
			if res then
				sticky.CFrame = CFrame.new(mouse.Hit.p, mouse.Hit.p + res.Normal)
				sticky.CFrame = sticky.CFrame + (sticky.CFrame.LookVector * (sticky.Size.Z/2))
				sticky.Orientation = Vector3.new(sticky.Orientation.X,sticky.Orientation.Y,rValue)
			else do
					sticky.Position = mouse.Hit.p + sticky.CFrame.LookVector * (sticky.Size.Z/2)
				end
			end
		end

Any help is appreciated, thanks.

You could try raycasting below where you want to place it to check if there is anything, and place the walls at the hit position but with like 1 more in the Y axis.

I don’t want it to go to the floor tho, I want it to be glued to what the mouse is currently hovering over