I am trying out a example on a book called Roblox Lua: Scripting For Beginners Leaderboard Script-Through. I am getting an error. The error is " Workspace.Part.Script:4: attempt to index nil with 'WaitForChild'". I am also getting that same error for FindFirstChild.
This is the leaderboard script:
game.Players.PlayerAdded:Connect(function (player)
stats = Instance.new("IntValue")
stats.Parent = player
stats.Name = "leaderstats"
points = Instance.new("IntValue")
points.Parent = stats
points.Name = "Points"
deaths = Instance.new("IntValue")
deaths.Parent = stats
deaths.Name = "Deaths"
end)
This is the script for the part that I am creating as the coin:
script.Parent.Touched:Connect(function()
player = game:GetService("Players").LocalPlayer
stats = player:WaitForChild("leaderstats")
points = stats:findFirstChild("Points")
points.Value = points.Value + 1
script.Parent:Destroy()
end)
When you step on this coin its suppose to add a point to the point column. I am trying to use FindFirstChild but I keep getting that NIL exception and I cant figure out why. I have done some research but seems like all the stuff that I have research are a little more complicated than my issue. The leaderstats does show up when I load into the game. The columns also show up as well (Points). Any ideas?
local. That will prevent polluting global namespace, unwanted interaction between functions, and also will speed up your script. - Alexander Mashin