How can I fix attempt to index nil with 'WaitForChild'?

Hi, in the console it says attempt to index nil with ‘WaitForChild’ can anyone help?

script.Parent.Triggered:Connect(function()
	local Player = game.Players.LocalPlayer
	local SampleGUI = Player:WaitForChild("PlayerGui").SampleGUI

	SampleGUI.Enabled = true
	
	for i = 1, 0, -0.01 do
		SampleGUI.Frame.BackgroundTransparency = i
		wait(0.01)
	end
	
	wait(2)
	
	SampleGUI.TextBox.TextTransparency = 0
	
	wait(2)
	
	SampleGUI.TextBox1.TextTransparency = 0
end) 

any help is appreciated

local SampleGUI = Player:WaitForChild(“PlayerGui”):WaitForChild(“SampleGUI”)

“Nil” means it can’t find it, so is it there or did you wait long enough for it to be there.

Rewrite:

task.wait(4) --> loading time (assumming the player isn't going to Trigger this till they can)
local Player = game.Players.LocalPlayer
local SampleGUI = Player:WaitForChild("PlayerGui")
:WaitForChild("SampleGUI") --> looks a bit sloppy but works well
--> (not sure how you're spelling this)

script.Parent.Triggered:Connect(function()
	
	SampleGUI.Enabled = true

	for i = 1, 0, -0.01 do
		SampleGUI.Frame.BackgroundTransparency = i
		task.wait(0.01)
	end

	task.wait(2)
	SampleGUI.TextBox.TextTransparency = 0

	task.wait(2)
	SampleGUI.TextBox1.TextTransparency = 0
end) 

Replace “PlayerGUI” with PlayerGui.

Spelling error?

2 Likes

It surly has a spelling syntax error here. But, it can also error due to timing on the load up. This script can take it’s time a bit as it will not be used until the player can use it. The task.wait(3-4) up top gives it loading time for SampleGUI to be there when referred to. Along with the way, it is called with the double WaitForChild() commands … this script is using both methods.

1 Like

the script does not work
idk why

bruh im dumb I need to put it in localscript

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.