Attempt to index nil ; Error only happens sometimes?

	local get_ui = plr:WaitForChild('PlayerGui')
	local get_framework = get_ui:FindFirstChild('UIFramework')
	local level_frame = get_framework['level_frame']

-- Error:

  15:58:32.393  ReplicatedStorage.GameModules.game_functions:144: attempt to index nil with 'level_frame'  -  Server - game_functions:144

This error only happens sometimes; if I join and click play to test in studio sometimes it won’t give this error and everything loads correctly, while other times I get this error and it stops my script from running.

image
image

There is proof that it exists, and is in the correct place.
This error is inconsitent, how so??

Is it that the WaitForChild is timed out after 5 seconds?

A simple way of troubleshooting is to print all the variables before the code calls for them to see if some or all of them are missing or nil.

when your game gets bigger with more assets added, it’ll begin to be noticable as it’s loading the content in the background, your UI might’ve been in process of being loaded before it returns as the instance you’re lookin for.

and as a result, your localscript runs and it still sees nil

a common fix that i use for my game is to make a custom StarterGUI and store all the screen gui in the ReplicatedStorage, it’s the only container that i believe will be replicated to the client first.

I dont have any local scripts running.

however i might resort to that, and cloning the screenUI so i have full control over it; awesome idea… ill let you know how it goes!!