How to make Build tool not collide with other items


How can i prevent this from happening?
I tried using the tutorial for a build tool but found it too difficult so i just made my own simpler version


local Player = game.Players.LocalPlayer
Buildmode = Player:GetAttribute("Buildmode")
local Mouse = Player:GetMouse()

while true do
	Buildmode = Player:GetAttribute("Buildmode")
	if Buildmode == true then
	
		block = game.ReplicatedStorage.SelectBox:Clone()
		block.Parent = workspace.Ignore
	
		Mouse.TargetFilter = workspace.Ignore
	else

	end
	while Buildmode == true do
		


		block.Position = Vector3.new(math.round(Mouse.Hit.Position.X) ,math.round(Mouse.Hit.Position.Y) + block.Size.Y / 2 ,math.round(Mouse.Hit.Position.Z) )
		
		wait(0.1)
	end
	wait(0.5)
end

This is the script i use to move the highlighted part.
Edit: What i want to achieve is for the part to not intersect with the walls