Wrong mouse position or etc?

I can’t fix this bug - then i target with some angle - block start jumping or etc.
https://i.imgur.com/9xLr9nq.gif

local Mouse = game.Players.LocalPlayer:GetMouse()
local Position = nil
local Limit = 50
local X = nil
local Y = nil
local Z = nil
local UIS = game:GetService("UserInputService")
local cooldown = false
local IsBuilding = script.Parent:WaitForChild("BuildConfigs").IsBuilding.Value
local BlockHiglight = Instance.new("Highlight")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local RotationY = 0
local RotationZ = 0
local RotationX = 0

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if IsBuilding == "True" then
			if cooldown == false then
				cooldown = true
				local distance = (Mouse.Hit.p - game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name).HumanoidRootPart.Position).Magnitude
				if distance < Limit then
					local Normal = Mouse.TargetSurface
					local hitblock = Mouse.Target
					game.ReplicatedStorage.RemoteEvents.Build:FireServer(Position,RotationX,RotationY,RotationZ)
				end
				wait(.2)
				cooldown = false
			end
		else
			if cooldown == false then
				cooldown = true
				local distance = (Mouse.Hit.p - game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name).HumanoidRootPart.Position).Magnitude
				if distance < Limit then
					local Normal = Mouse.TargetSurface
					local hitblock = Mouse.Target
					if hitblock.Parent:FindFirstChild("BlockOwner") then
						BlockHiglight.Parent = nil
						game.ReplicatedStorage.RemoteEvents.Delete:FireServer(hitblock.Parent)
					end
				end
				wait(.2)
				cooldown = false
			end
		end
	end
	if input.KeyCode == Enum.KeyCode.R then
		RotationY = RotationY + 1.57
		if RotationY == 6.28 then
			RotationY = 0
		end
	elseif input.KeyCode == Enum.KeyCode.E then
		RotationX = RotationX + 1.57
		if RotationX == 6.28 then
			RotationX = 0
		end
	elseif input.KeyCode == Enum.KeyCode.T then
		RotationZ = RotationZ + 1.57
		if RotationZ == 6.28 then
			RotationZ = 0
		end
	end
end)

script.Parent.BuildConfigs.Delete.MouseButton1Click:Connect(function()
	if IsBuilding == "True" then
		IsBuilding = "False"
		if game.Workspace.Blocks:FindFirstChild("BlockEffect") then
			game.Workspace.Blocks:FindFirstChild("BlockEffect"):Destroy()
		end
		script.Parent.BuildConfigs.Delete.Checkmark.Visible = true
	else
		IsBuilding = "True"
		BlockHiglight.Parent = nil
		script.Parent.BuildConfigs.Delete.Checkmark.Visible = false
	end
end)


while true do
	if IsBuilding == "True" then
		local GridSize = 3
		X = math.floor((Mouse.Hit.X + GridSize / GridSize) / GridSize) * GridSize
		Y = math.ceil((Mouse.Hit.Y + GridSize / GridSize ) / GridSize) * GridSize - 1.5
		Z = math.floor((Mouse.Hit.Z + GridSize / GridSize) / GridSize) * GridSize
		Position = CFrame.new(X, Y, Z)
		local target = Mouse.Target
		if target ~= nil and  target~= "Center" then
			if game.Workspace.Blocks:FindFirstChild("BlockEffect") then
				game.Workspace.Blocks:FindFirstChild("BlockEffect"):Destroy()
			end
			local Normal = Mouse.TargetSurface
			if Normal == Enum.NormalId.Bottom then
				Position += Vector3.new(0,-3,0)
			end
			if Normal == Enum.NormalId.Right then
				Position += Vector3.new(3,0,0)
			end
			if Normal == Enum.NormalId.Back then
				Position += Vector3.new(0,0,3)
			end
			local distance = (Mouse.Hit.p - game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name).HumanoidRootPart.Position).Magnitude
			if distance < Limit then
				local shape = game.ReplicatedStorage.CurrentBlocks:FindFirstChildOfClass("Model"):Clone()
				shape:SetPrimaryPartCFrame(Position)
				shape:SetPrimaryPartCFrame(shape.Center.CFrame * CFrame.Angles(RotationX, RotationY, RotationZ))
				shape.Name = "BlockEffect"
				for i, v in pairs(shape:GetChildren()) do
					if v:IsA("Part") and v.Name ~= "Center" then
						v.CanQuery = false
						v.Transparency = 0.5
						v.CanCollide = false
					end
				end
				shape.Parent = game.Workspace.Blocks
			end
		end
	else
		local Target = Mouse.Target
		if Target ~= nil then
			local distance = (Mouse.Hit.p - game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name).HumanoidRootPart.Position).Magnitude
			if distance < Limit then
				if Target.Parent:FindFirstChild("BlockOwner") then
					BlockHiglight.Parent = Target.Parent
				else
					BlockHiglight.Parent = nil
				end
			end

		end
	end
	wait(0.01)
end


Your red brick becomes the new Mouse.Target when you place it underneath the mouse.

Your options:

  1. Replace mouse.Target with a raycast + ignore list, or
  2. Set Mouse.TargetFilter to be the red block (or one of its parents)