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
local inventory
print(#inventory)
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
- The table was never created, or its variable name is misspelled.
- A function that should return a table returned nothing.
- 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.