In short
#x gives the length of a table or a string. attempt to get length of a nil value means x was nil on that line, so there was nothing to measure. Find out why the table is missing (data that has not loaded, a function that returned nothing, a key with a typo) and check it first, or use #(x or {}) when an empty list is a fine answer.
#works on tables and strings only.a nil valuemeans the table was never there; any other type at the end means you measured the wrong thing.- To count the children of a folder, use
#folder:GetChildren(), not#folder.
What the message means
ServerScriptService.Inventory:12: attempt to get length of a nil value
Other forms of the same message:
attempt to get length of a number valueattempt to get length of a boolean value
The length operator # counts the items in a list (#{"a", "b"} is 2) or the characters in a string (#"abc" is 3). Any other value has no length, and Luau stops the script on that line.
The type at the end of the message is what Luau found. With nil, look at where the table should have come from. With an object type, you measured an object such as a Folder instead of the list GetChildren() returns.
Causes and fixes
Three things cause this error in practice. Each one below has the smallest code that triggers it, then the fix.
- Player data has not loaded yet
- An object was measured instead of its children
- A function or a decode returned nothing
Player data has not loaded yet
If a player's table only exists after a data store load, any code that runs earlier (a shop opening, a RemoteEvent, a leaderboard update) measures nil.
local inventories = {}
local function countItems(userId)
-- before the load sets inventories[userId]: attempt to get length of a nil value
return #inventories[userId]
end
print(countItems(12345))
Check the table first. Return a clear answer for "not loaded yet" instead of guessing, or fall back to an empty table when zero is correct.
local inventories: { [number]: { string } } = {}
local function countItems(userId: number): number?
local inventory = inventories[userId]
if inventory == nil then
return nil -- not loaded yet
end
return #inventory
end
print(countItems(12345))
An object was measured instead of its children
A Folder or a Model is an Instance, not a list, so #workspace.Enemies fails too, with the object's type at the end of the message instead of nil. The list of what is inside comes from GetChildren().
local enemies = workspace:WaitForChild("Enemies")
-- fails: a Folder has no length
print("enemies left:", #enemies)
Count the list that GetChildren() returns. Use GetDescendants() if the parts are inside models.
local enemies = workspace:WaitForChild("Enemies")
print("enemies left:", #enemies:GetChildren())
A function or a decode returned nothing
A helper that forgets return gives back nil, and so does a call wrapped in pcall when it failed. Measuring that result fails here instead of where the real problem is.
local DataStoreService = game:GetService("DataStoreService")
local store = DataStoreService:GetDataStore("Quests")
local function loadQuests(userId)
local ok, quests = pcall(function()
return store:GetAsync(tostring(userId))
end)
-- a new player has no saved quests: GetAsync returns nil
print("quests:", #quests, ok)
end
loadQuests(12345)
Check both the pcall result and the type of what came back. A new player with nothing saved gets nil from GetAsync, so start them with an empty table.
local DataStoreService = game:GetService("DataStoreService")
local store = DataStoreService:GetDataStore("Quests")
local function loadQuests(userId: number): { string }?
local ok, result = pcall(function()
return store:GetAsync(tostring(userId))
end)
if not ok then
warn("[Quests] load failed:", result)
return nil
end
if typeof(result) ~= "table" then
return {} -- new player
end
return result
end
local quests = loadQuests(12345)
if quests then
print("quests:", #quests)
end
Fix it in your place
Paste the error, RoCode fixes it in your place
Copy the line from Output and paste it here. RoCode opens the script it names in the place you have open in Studio, works out which of the causes above it is, and edits the code there instead of handing you a snippet to paste.
RoCode is an AI agent for Roblox Studio: it reads the scripts in the place you have open, finds the line and edits it there, and every change is an undo point. It runs on Windows or macOS with a desktop browser. Free to start, with free credits every day.
How to find the line that causes it
- Open Output (Window menu, or the Script tab), clear it with Ctrl+K (⌘K on Mac), and press Play.
- Find the first red line. It starts with the script's place in the Explorer and the line number, such as
ServerScriptService.Shop:12:. - Click the red line. Studio opens that script with the cursor on the failing line.
- Read the lines between
Stack BeginandStack Endunder it: they list the calls that led there, newest first, so you can see which function called the broken one. - Still not sure which value is wrong? Add
print(typeof(x), x)just above the line, or set a breakpoint on it and read the values in the Watch window.
Questions
Does # work on dictionaries?
It works but does not count the keys. # counts the list part (keys 1, 2, 3 and so on). To count keys such as names, loop over the table and add one each time.
Why does #folder not work?
A Folder is an Instance, not a table. #folder:GetChildren() counts what is inside it.
What is the difference from attempt to index nil?
Same root cause, different operation. attempt to index nil means you read a key from nil; this one means you measured nil with #.
Can RoCode fix it?
Yes. Paste the Output line into RoCode. It reads the script in your open place, finds why the table is missing and adds the check.
Sources and further reading
- Roblox Creator Docs: tables
- Roblox Creator Docs: operators
- Roblox Creator Docs: GlobalDataStore:GetAsync
How the code was checked: every block marked Fixed passes Luau's strict type checker against Roblox's API definitions (luau-lsp, 14 November 2026), and every block marked Breaks compiles and fails only when it runs. The message wording was checked by running the broken code with the Luau command line tool and against the message strings in Roblox Studio itself (macOS build installed 12 November 2026) on 14 November 2026. A type check catches misspelt APIs and wrong types, not game logic, so playtest in Studio before you publish.