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

Script works, but outputs "Attempt to index nil with `FindFirstChild`when looking at sky?

Asked by 4 years ago
Edited 4 years ago

The script (localscript) works as intended, but whenever I playtest, it outputs "Attempt to index nil with FindFirstChild, (only when looking at the sky.) This makes it next to impossible to read anything in the output, since it's being occupied by a gazillion of these errors. Here's the script, hope someone can help:

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

mouse.Move:Connect (function() local target = mouse.Target

if target:FindFirstChild ("dex") then
    target.dex.Enabled = true
    if target.Transparency == 1 then
        target.dex.Enabled = false
    end
elseif target:FindFirstChild ("Berry") then
    target.Berry.dex.Enabled = false
end     

end)`

I'm a rookie at coding, so don't judge if the code's terrible ????

0
Whatever target is, it's nil User#834 0 — 4y
0
flowery monkey is right. whatever target is, its nil or its an object that doesnt have the function :FindFirstChild() Zeuxulaz 148 — 4y
0
Then how may I formulate the code differently as to prevent these errors?? ROOBLOXJakob123 22 — 4y
0
You need to find what is causing target to be nil, where does it originate from? Trace back what could cause it to be nil. User#834 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

the problem is that you not checking if target exists, when u look at the sky, There's nothing (unless theres a part in the sky) so target is nil, to fix it, add another if statement to check if target exists and if it has a parent

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

mouse.Move:Connect (function()
    if mouse.Target then -- check if there's a target else it'll error
        local target = mouse.Target
    end
end

if target and target.Parent then
    if target:FindFirstChild ("dex") then
        target.dex.Enabled = true
        if target.Transparency == 1 then
            target.dex.Enabled = false
        end
        elseif target:FindFirstChild ("Berry") then
            target.Berry.dex.Enabled = false
        end
    end
end
0
Wow, it was that easy huh, and you spotted it right away. Thank you so, so much. I appreciate you taking your time to explain a simple problem to a rookie like me. By using your new and improved script, I can actually see the other error messages that I need to focus on. Again, I am ever so grateful. Take care xD! ROOBLOXJakob123 22 — 4y
Ad

Answer this question