Attempt to index nil with 'WaitForChild'

Hi, pls help I dont know what to do it says attempt to index nil with wait for child
here is the script:

local Player = game:GetService("Players").LocalPlayer
local Money = Player:WaitForChild("leaderstats"):WaitForChild("Money")

script.Parent.Triggered:Connect(function()
	if Money.Value == 15 then
		print("Money works")
	end
end)

any help is appreciated

3 Likes

Try doing this:

local Player = game:GetService("Players").LocalPlayer
local LeaderstatsFolder = Player:WaitForChild("leaderstats")
local Money = LeaderstatsFolder:WaitForChild("Money")

I’m not sure if this is the solution, but go run it and if it gives an error tell me what the error is and on what line it occured.

It sasy attempt to index nil with waitforchild in this line:

local LeaderstatsFolder = Player:WaitForChild("leaderstats")

Maybe this will work?

script.Parent.Triggered:Connect(function(Player)
	local Money = Player:WaitForChild("leaderstats"):WaitForChild("Money")
	if Money.Value == 15 then
		print("Money works")
	end
end)

What type of script are you using?

1 Like

Try not to use concatenated functions with no check on the return.

local Money = Player:WaitForChild("leaderstats"):WaitForChild("Money")

Split this of into two statements as @RogueBloxxerr suggested.
You can also use a timeout on WaitForCHild() using the second parameter. This gives you an opportunity to accommodate replication times.

local Money = Player:WaitForChild("leaderstats",3); -- this will keep trying to obtain the child for 3 seconds

Is this a serverscript or a localscript? It’s saying LocalPlayer is nil, not leaderstats

If this is a server script, the reason it doesn’t work is because there’s no LocalPlayer on the server. You can either:

  1. Put the script into a LocalPlayer in StarterPlayerScripts.
    or 2. Change the script to this:
script.Parent.Triggered:Connect(function(player)
	local Money = player:WaitForChild("leaderstats"):WaitForChild("Money")
	if Money.Value == 15 then
		print("Money works")
	end
end)

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