attempt to get length of a nil valuein Roblox Luau: what it means and how to fix it

What it means

You used # to count the items in something that is nil. The length operator only works on tables and strings.

The smallest script that triggers it

Triggers the errorline 2: error here
local inventory
print(#inventory)

The fix

Fixedline 1: the fix
local inventory = {}
print(#inventory)

An empty table has a length of 0. A variable with no value has no length at all.

Common causes

  1. The table was never created, or its variable name is misspelled.
  2. A function that should return a table returned nothing.
  3. A nested field is missing, for example `#data.items` when data has no items.

Practice this until it sticks

A Deep Dive on tables and the length operator: 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.