Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Error With WaitForChild() On Leaderstats?

I have a error that says "ServerScriptService.Script:7: attempt to index nil with 'WaitForChild'"

game.Players.PlayerAdded:Connect(function() local leader = Instance.new('Folder') leader.Name = 'leaderboard' leader.Parent = game.Players.LocalPlayer local leadera = Instance.new('IntValue') leadera.Name = 'Cash' leadera.Parent = game:GetService("Players").LocalPlayer:WaitForChild("leaderboard") leadera.Value = '0' end)

2 answers

✓ Accepted answer

Multiple things are wrong with this script. Local Player doesn't work in server side scripts. This looks like it's a Server side script.

Also its "leaderstats" not "leaderboard"

replace what you have with the following, or change it to look like this:

game.Players.PlayerAdded:Connect(function(plr) 
	local leader = Instance.new('Folder') 
	leader.Name = 'leaderstats' 
	leader.Parent = plr
	local leadera = Instance.new('IntValue') 
	leadera.Name = 'Cash' 
	leadera.Parent = leader 
	leadera.Value = '0' 
end)

Hope this helps!

2 comments

0

thank you! it worked!

0

accepting this answer since you replied it worked

game.Players.LocalPlayer can only be accessed by a local script and player joined event gives you the player object itself so instead try this:

game.Players.PlayerAdded:Connect(function(plr) 
	local leader = Instance.new('Folder')
	leader.Name = "leaderstats" 
	leader.Parent = plr
	local leadera = Instance.new('IntValue')
	leadera.Name = "Cash"
	leadera.Parent = leader
	leadera.Value = 0
	
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.