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

attempt to get length of a nil value | Roblox dev tutorial has incorrect code?

Asked by 3 years ago

Working on this beginner guide on scripting: https://developer.roblox.com/en-us/onboarding/scoring-points/2

Either I am messing up this code or the guide is wrong? LOL.

The error is: ServerScriptService.Script:19: attempt to get length of a nil value

This script is supposed to create the leaderboard with a category for points, and add 1 point to each individual player every second for as long as they stay alive.

local Players = game:GetService("Players")

local function onPlayerAdded(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local points = Instance.new("IntValue")
    points.Name = "Points"
    points.Value = 0
    points.Parent = leaderstats
end

Players.PlayerAdded:Connect(onPlayerAdded)

while true do
    wait(1)
    local playerList = Players:GetPlayers()
    for currentPlayer = 1, #playerlist do
        local player = playerList[currentPlayer]
        local points = player.leaderstats.Points
        points.Value = points.Value + 1
    end
end

3 answers

Log in to vote
2
Answered by
2_MMZ 1059 Moderation Voter
3 years ago

You're getting a nil value because it wasn't identified properly. You have it as #playerlist, but as stated from your local, you set it to be #playerList (capital L). You need to change the lowercase l to a capital L. Just a simple but hard to see error. You'll need to change line 19 to this:

for currentPlayer = 1, #playerList do

We can see that you have an error that comes out in the output, the number 19 in it. This clarifies that there has been an error in line 19.

Ad
Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

You spelled playerList wrong... It's supposed to be #playerList not #playerlist

0
Line 19 by the way Xapelize 2658 — 3y
Log in to vote
0
Answered by 3 years ago
while true do
    wait(1)
    local playerList = Players:GetPlayers()
    for currentPlayer = 1, #playerlist do
        local player = playerList[currentPlayer]
        local points = player.leaderstats.Points
        points.Value = points.Value + 1
    end
end

is wrong it has to be

while true do
    wait(1)
    local playerList = Players:GetChildren()
    for i, v in ipairs(playerList) do
        local player = v
        local points = player.leaderstats.Points
        points.Value = points.Value + 1
    end
end

For more look at https://developer.roblox.com/en-us/articles/Table

The article you are coding is probably old.

2
GetChildren returns everything on the Players service, including things that is not players. And GetPlayers returns a list of table, you can check GetPlayers return values. Anyways, good try! Xapelize 2658 — 3y
0
you could just do if v:IsA("Player") GiantDefender427 0 — 3y
1
indeed, but GetPlayers() already does that for you. internally. use whatcha got :) Speedmask 661 — 3y

Answer this question