0
votes

Workspace.Part.Script:16: attempt to index local 'screengui' (a nil value)

wait(2) -- Testing to make sure assets loaded

script.Parent.Touched:connect(function(hit)
    if not hit or not hit.Parent then return end
    local human = hit.Parent:findFirstChild("Humanoid")
    if human and human:IsA("Humanoid") then
        local person = game.Players:GetPlayerFromCharacter(human.parent)
        if not person then return end
        person.Checklist.FirstEggCollected.Value = true
        local playgui = person:FindFirstChild('PlayerGui')
        print(playgui)
        wait(0.2)
        local screengui = playgui:FindFirstChild('ScreenGui')
        wait(0.2)
        print(screengui) -- This prints nil
        local collectnotice = screengui:FindFirstChild('CollectionNotice') -- This is line 16
        local Toggle = collectnotice.Toggle
        local text = Toggle.Text
        local value = text.Value
        value = "The Easy Egg!"
        person:WaitForChild('PlayerGui'):FindFirstChild('ScreenGui'):FindFirstChild('CollectionNotice').Toggle.Color.Value = Color3.fromRGB(0,255,0)
        person:WaitForChild('PlayerGui'):FindFirstChild('ScreenGui'):FindFirstChild('CollectionNotice').Toggle.Value = true
        script.Parent:Destroy()
        wait(5)
        game.Workspace.Variables.IfFirstEggInGame.Value = false
    end
end)

I've been at this for hours. No idea how to make the error fix. FE is on, Yes its name is "ScreenGui" and it is inside "PlayerGui"

Error: Workspace.Part.Script:16: attempt to index local 'screengui' (a nil value)

1
Oh, and btw, It works perfectly in Studio, but not in the Game. - King Maniac

1 Answers

0
votes

From the Roblox manual: http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild

Description: Returns the first child found with the given name, or nil if no such child exists.

So there seems to be no child named "ScreenGui".

If a function may return nil you have to handle that properly. Blindly indexing possible nil values is bad practice.