Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Mouse.Target still returning nil and throwing an error after a check?

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?

1 answer

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)

2 comments

0

That was just a typo on the website, my bad :)

0

Fixed

This discussion is preserved as it was. New answers, comments, and votes are closed.