Having issue's with the player's mouse

Does anyone know why this script is not working?
I want to make it so that the player mouse can drag parts, other than unlisted blocks and can move then around the map.
I have two issues, I keep getting this error

  19:38:28.967  Players.NubblyFry.PlayerScripts.LocalScript:13: attempt to index nil with 'ClassName'  -  Client - LocalScript:13
  19:38:28.967  Stack Begin  -  Studio
  19:38:28.967  Script 'Players.NubblyFry.PlayerScripts.LocalScript', Line 13  -  Studio - LocalScript:13
  19:38:28.967  Stack End  -  Studio

And Also, the blocks always go close to the player screen.

Script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mouseHold = false

local cannotHoldTable = {nil, "Baseplate"}

mouse.Move:Connect(function()
	if mouseHold then
		local Target = mouse.Target
		
		for i, v in pairs(cannotHoldTable) do
			if Target ~= v then
				if Target.ClassName == "Part" or "UnionOperation" then
					Target.Position = mouse.Hit.Position
				end
			end
		end
	else
		if not mouseHold then
			-- Do nothing
		end
	end
end)

mouse.Button1Down:Connect(function()
	mouseHold = true
end)

mouse.Button1Up:Connect(function()
	mouseHold = false
end)

add an if Target then before the loop.

1 Like

use Target:IsA("Part") or Target:IsA("UnionOperation")

1 Like