I have a program written where every time the mouse is moved, the script checks to see if the mouse is targeting certain blocks. The weird thing is, it will randomly throw and error saying the field Target is nil even though I'm checking to make sure it isn't! Here is a snippet of the code:
local mouse = game.Players.LocalPlayer:GetMouse() mouse.Move:connect(function() if mouse.Target ~= nil and mouse.Target == partToCheckFor then --code end end)
(partToCheckFor is a place holder for a variable I used in my program)
Is there something I could do to make it not throw this error randomly, or is there at least a way I could work around it?
the reason it does not work is because you used if do instead of if then if do will result in error please use if then like in the code below
local mouse = game.Players.LocalPlayer:GetMouse() mouse.Move:connect(function() if mouse.Target ~= nil then if mouse.Target == partToCheckFor then --code end end end)