Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

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

Log in to vote
0
Answered by 9 years ago

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)
0
That was just a typo on the website, my bad :) BlackJPI 2658 — 9y
0
Fixed DragonSkyye 517 — 9y
Ad

Answer this question