Attempt to index nil with 'Main'

The Title Explains

local player = game:GetService("Players").LocalPlayer

player.CharacterAdded:Wait()

local ui = player.PlayerGui

local UPDATE_LABEL_COOLDOWN = 0.1

local SettingUI = ui:FindFirstChild("SettingsUI")

local SettingMain = SettingUI.Main

Errors on line 7

Try utilizing WaitForChild; this waits for the child to be added before proceeding.

local SettingUI = ui:WaitForChild("SettingsUI") 

FindFirstChild returns nil if it’s unable to find the child when it’s called.

1 Like

It worked! also I used player.CharacterAdded:Wait() in line 2 but why did it still not work before?

From the error, I can assume that when the script was loaded into the game, the SettingsUI Instance hadn’t been replicated yet and didn’t exist at the time of the script running (which is why it returned nil).

player.CharacterAdded:Wait() just waits for the player’s character to be added into the game, it doesn’t wait for the other objects in the game to finish loading

1 Like