Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Error With WaitForChild() On Leaderstats?

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

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!

0
thank you! it worked! TimoteoRoblox 10 — 3y
0
accepting this answer since you replied it worked greatneil80 2647 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)

Answer this question