Hello, I have a problem with the build system and I would like to ask for help. You can see my problem below:

As you can see in the gif, when I move the cursor towards the wall, part goes through wall, and I don’t know what to do with it.
I can assume it can be done with raycasting, but I have no idea how to do it.
My script:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
equipped = false
script.Parent.Equipped:Connect(function()
equipped = true
local Block = game.ReplicatedStorage.Block:Clone()
Block.Parent = workspace
Mouse.TargetFilter = Block.Root
while true do
wait()
if equipped == true and Block.Root then
local NewPos = Vector3.new(math.floor(Mouse.Hit.X + .5), math.floor(Mouse.Hit.Y + 1.5), math.floor(Mouse.Hit.Z + .5))
if NewPos then
game:GetService("TweenService"):Create(Block.Root, TweenInfo.new(0.1), {CFrame = CFrame.new(NewPos)}):Play()
end
else
Block:Destroy()
end
end
end)
script.Parent.Unequipped:Connect(function()
equipped = false
end)
(I removed unnecessary parts from script and left only the necessary)
Also, here are a few words about the script:
- This is a LocalScript
- LocalScript Parent - tool.
- The
ReplicatedStoragecontains a Model, namedBlock, Model contains a Part, namedRoot.
Thank you in advance!