Attempt to index field 'Parent' (a nil value)

scripting
bug

#1

In my game sometimes, very rarely, a very curious error comes out that I don’t understand why it happens.

“Players.Player.PlayerGui.LobbyGui.Frames.Inventory.Inventory:67: attempt to index field ‘Parent’ (a nil value)” and “Players.Player.PlayerGui.LobbyGui.Frames.Shop.Shop:69: attempt to index field ‘Parent’ (a nil value)”

This error comes out in the script of the Inventory and Shop gui in exactly the same way.

local inventoryButton = frame.Parent.Parent:WaitForChild("Buttons"):WaitForChild("InventoryButton")
inventoryButton.MouseButton1Down:connect(otherButtonDown)

local shopButton = frame.Parent.Parent:WaitForChild("Buttons"):WaitForChild("ShopButton")
shopButton.MouseButton1Down:connect(otherButtonDown)

local spectateButton = frame.Parent.Parent:WaitForChild("Buttons"):WaitForChild("SpectateButton")
spectateButton.MouseButton1Down:connect(otherButtonDown)

local configButton = frame.Parent.Parent:WaitForChild("Buttons"):WaitForChild("ConfigButton") --ERROR
configButton.MouseButton1Down:connect(otherButtonDown)

That portion of the Script is in both the Inventory and Shop Script, and here is an image of the Explorer.
asd

The error comes out in the line with “local configButton”, what seems strange to me is that it doesn’t come out in the previous ones, which also have “script.Parent.Parent” but they works.

Does anyone know why this is happening? I’ve checked and the Parent is never changed.


#2

You’re probably running into some sort of replication-lag. How are you defining frame? Is it defined using script.Parent or player.PlayerGui:WaitForChild()?

If it’s script.Parent, changing it to something like this might be a good idea.

local pg = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local lobbyScreen = pg:WaitForChild("LobbyGui")
local frames = lobbyScreen:WaitForChild("Frames")
local frame = frames:WaitForChild("Inventory"):WaitForChild("Frame")

and then I would also change your frame.Parent.Parent references to frames