0
votes

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?

1
A side note: always declare your variables and non-method functions as local. That will prevent polluting global namespace, unwanted interaction between functions, and also will speed up your script. - Alexander Mashin
developer.roblox.com/en-us/api-reference/property/Players/… says: "This property is only defined for LocalScripts (and ModuleScripts required by them), as they run on the client. For the server (on which Script objects run their code), this property is nil. See Roblox Client-Server Model for more information on game networking on Roblox." Are you sure this is not your case? - Alexander Mashin
I would believe this is my case. I will check it out. Though this book was published and I would believe the way they set up their examples, they would be correct. I understand some of them would be out of date but this isn't. Just a rant. Not to waste your time. Thank you for the link. I will check it out real fast. - Kin
@AlexanderMashin So I tried out the whole local script and looked out through the documentation and that seems like its not working. I added a local script to the part but it didnt run. Do you have any ideas on what might be going on? - Kin

1 Answers

0
votes

OK there are a few things you must change to let the code works, let me help you.

As Alexander said you need to use local in variables, also declare the value for the int values, a recommendation too would be to use Instance.new("IntValue",parent) instead of using a new line. Also for the leaderstats it is better to use a folder instead of a value.

game.Players.PlayerAdded:Connect(function (player) 
    local stats = Instance.new("Folder",player)
    stats.Name = "leaderstats"
    
    local points = Instance.new("IntValue",stats)
    points.Value = 0
    points.Name = "Points"
    
    local deaths = Instance.new("IntValue",stats)
    deaths.Value = 0
    deaths.Name = "Deaths"
end)

And for touched parts as well, here the main problem is that will be nil always cause you are not receiving the parameter from touched, also you may want to place that script as server side, and not localscript, and not use localplayer otherwise it will have no effect on the server, and no one will be able to get points.

script.Parent.Touched:Connect(function(hit) --hit is the exact part that touch
    player = game.Players:FindFirstChild(hit.Parent.Name) --look for the parents name of the part
    if player then --detect if what touched the part is a player
        local stats = player:WaitForChild("leaderstats")
        local points = stats:FindFirstChild("Points")
        points.Value = points.Value + 1
        script.Parent:Destroy()
    end
end)