In this script, everything works great except for when the mouse's target is nil. I'm creating a building game and I would appreciate it if someone could tell me how to fix this.
A video of it happening: (https://photos.app.goo.gl/ryZwdLLu1WmFbPV26)
Code:
local player = game.Players.LocalPlayer local mouse = player:GetMouse() script.Parent.Equipped:Connect(function() e = true if player.Character:FindFirstChild('ShowPart') == nil then showpart = Instance.new('Part') showpart.Parent = player.Character showpart.Anchored = true showpart.Size = Vector3.new(4, 4, 4) showpart.CanCollide = false showpart.Name = "ShowPart" showpart.Transparency = 1 end if mouse.Target.Name == player.Name .."Parts" then showpart.Transparency = 0 elseif mouse.Target:FindFirstChild('Player') ~= nil then if mouse.Target.Player.Value == player.Name then showpart.Transparency = 0 end end mouse.Icon = 'rbxasset://textures/ArrowCursor.png' while mouse.Target ~= nil and mouse.Target.Parent.Name ~= _G. playerplate do wait() if mouse.Target ~= nil and mouse.Target:FindFirstChild('Player') ~= nil then if mouse.Target.Player.Value == player.Name then showpart.Transparency = 0.6 showpart.Rotation = Vector3.new(0, 0, 0) if mouse.Target ~= nil and mouse.Target:FindFirstChild('Player') ~= nil then if mouse.Target.Player.Value == player.Name then local x = math.floor(mouse.Hit.p.X / 4) * 4 local y = mouse.Hit.p.Y + 2 local z = math.floor(mouse.Hit.p.Z / 4) * 4 showpart.CFrame = CFrame.new(Vector3.new(x,y,z) + Vector3.new(2, 0, 2)) end end end end end while mouse.Target ~= nil and mouse.Target:FindFirstChild('Player') == nil do wait() showpart.Transparency = 0.6 if mouse.Target.Parent.Name == player.Name .."Parts" then if mouse.TargetSurface == "BackSurface" then showpart.CFrame = mouse.Target.CFrame - Vector3.new(0, 0, 4) elseif mouse.TargetSurface == "BottomSurface" then showpart.CFrame = mouse.Target.CFrame - Vector3.new(0, 4, 0) elseif mouse.TargetSurface == "FrontSurface" then showpart.CFrame = mouse.Target.CFrame + Vector3.new(0, 0, 4) elseif mouse.TargetSurface == "LeftSurface" then showpart.CFrame = mouse.Target.CFrame - Vector3.new(4, 0, 0) elseif mouse.TargetSurface == "RightSurface" then showpart.CFrame = mouse.Target.CFrame + Vector3.new(4, 0, 0) elseif mouse.TargetSurface == "TopSurface" then showpart.CFrame = mouse.Target.CFrame + Vector3.new(0, 4, 0) end end end repeat wait() if mouse.Target ~= nil then if mouse.Target:FindFirstChild('Player') == nil then if mouse.Target.Parent ~= _G. playerparts then showpart.Transparency = 1 end else if mouse.Target.Player.Value == player.Name then showpart.Transparency = 0.6 end end end until e == false end) script.Parent.Unequipped:Connect(function() e = false mouse.Icon = "rbxasset://textures/ArrowFarCursor.png" showpart.Transparency = 1 end) script.Parent.Activated:Connect(function() game.ReplicatedStorage.Build:FireServer(player, showpart.CFrame) end)