players[0] is nilin Roblox Luau: what it means and how to fix it

What it means

Luau lists start at 1, not 0. Nothing is stored at index 0, so reading players[0] gives nil without any error. The error comes one line later, when the script uses that nil.

The same error also shows up as:

  • attempt to index nil with 'Name'

Prefer to watch? Why players[0] is nil in Luau: tables start at 1 (25 seconds, on YouTube).

The smallest script that triggers it

Triggers the errorline 5: error here
local Players = game:GetService("Players")
 
local players = Players:GetPlayers()
local first = players[0]
print(first.Name)

The fix

Fixedline 4: the fix
local Players = game:GetService("Players")
 
local players = Players:GetPlayers()
local first = players[1]
if first then
  print(first.Name)
end

The first item of a Luau array is at index 1 and the last is at #players. The if covers a server with no players yet.

Common causes

  1. A habit from Python, JavaScript or C#, where lists start at 0.
  2. A for loop written as `for i = 0, #list - 1`, which reads index 0 and misses the last item.
  3. An index calculated with % (remainder), which gives 0 for exact multiples.

Practice this until it sticks

A Deep Dive on tables and array indexes: 5-minute lessons, each with a quiz. What you miss comes back in the next module. One Deep Dive a day is free. No card.