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

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

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

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.

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

1 comment

0

Line 19 by the way

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.

3 comments

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!

0

you could just do if v:IsA("Player")

1

indeed, but GetPlayers() already does that for you. internally. use whatcha got :)

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