In short
t[key] = value needs a real key. table index is nil means key was nil on that line, so Luau had nowhere to store the value. The table is fine; the key is missing. Find out why it is nil (a player that already left, a FindFirstChild that found nothing, an attribute that is not set) and check it before you write.
- The problem is the key in the square brackets, not the table.
- Only writing fails. Reading
t[nil]quietly givesnil, which hides the same bug elsewhere. table index is NaNis the same mistake with a number key that came from0/0.
What the message means
ServerScriptService.Coins:14: table index is nil
Other forms of the same message:
table index is NaN
A Luau table maps keys to values. Any value can be a key except nil (and NaN, the result of 0/0), because a nil key could never be found again. So scores[nil] = 10 stops the script with this error.
The line number points at the assignment. Look at what is inside the square brackets on that line, or at the left side of key = value in a table you are building with { [key] = value }.
Causes and fixes
Three things cause this error in practice. Each one below has the smallest code that triggers it, then the fix.
- The key came from a lookup that found nothing
- The key is an attribute or value that is not set
- A number key became NaN
The key came from a lookup that found nothing
FindFirstChild, GetPlayerFromCharacter and similar calls return nil when there is no match. Using the result straight away as a key fails on the write.
local Players = game:GetService("Players")
local touchesByPlayer = {}
workspace.Coin.Touched:Connect(function(hit)
-- a falling part or an NPC touched the coin: player is nil
local player = Players:GetPlayerFromCharacter(hit.Parent)
touchesByPlayer[player] = (touchesByPlayer[player] or 0) + 1
end)
Check the result and return early when it is nil. Most touch handlers need this anyway, because parts touch parts.
local Players = game:GetService("Players")
local touchesByPlayer: { [Player]: number } = {}
local coin = workspace:WaitForChild("Coin") :: BasePart
coin.Touched:Connect(function(hit: BasePart)
local character = hit.Parent
local player = if character and character:IsA("Model") then Players:GetPlayerFromCharacter(character) else nil
if player == nil then
return -- not a player's character
end
touchesByPlayer[player] = (touchesByPlayer[player] or 0) + 1
end)
The key is an attribute or value that is not set
GetAttribute returns nil for an attribute nobody set, often because it was typed in the Properties window with a different spelling. A StringValue that was never filled in gives an empty string instead, which works as a key but is still wrong.
local stock = {}
for _, item in workspace.ShopItems:GetChildren() do
-- the attribute is called "ItemId" on the parts, not "Id": key is nil
stock[item:GetAttribute("Id")] = item
end
Read the attribute into a local, check its type, and warn with the object name so you can find the one that is missing it.
local stock: { [string]: Instance } = {}
local shopItems = workspace:WaitForChild("ShopItems")
for _, item in shopItems:GetChildren() do
local id = item:GetAttribute("ItemId")
if typeof(id) == "string" and id ~= "" then
stock[id] = item
else
warn(`[Shop] {item:GetFullName()} has no ItemId attribute`)
end
end
A number key became NaN
Dividing zero by zero gives NaN ("not a number"), and NaN is not allowed as a key either. This shows up as table index is NaN, for example when a level or a grid cell is worked out from an average with no entries yet.
local rewardsByLevel = {}
local totalXp = 0
local rounds = 0
-- 0 / 0 is NaN: table index is NaN
local averageLevel = math.floor(totalXp / rounds)
rewardsByLevel[averageLevel] = "Starter Pack"
Guard the division, so the key is always a real number.
local rewardsByLevel: { [number]: string } = {}
local totalXp = 0
local rounds = 0
local averageLevel = if rounds > 0 then math.floor(totalXp / rounds) else 0
rewardsByLevel[averageLevel] = "Starter Pack"
print(rewardsByLevel)
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
Why does reading t[nil] not error?
Luau allows the read and returns nil, because no key can be nil. Only the write fails. That is why the same bug can hide for a while and then appear on the line that saves something.
Can I use a Player object as a table key?
Yes. Any value except nil and NaN can be a key, including a Player or a Part. Remove the entry in Players.PlayerRemoving so the table does not keep players that left.
Is it the same as attempt to index nil?
No. attempt to index nil with 'X' means the table itself was nil. table index is nil means the table exists and the key was nil.
Can RoCode fix it?
Yes. Paste the Output line into RoCode. It reads the script in your open place, finds where the key comes from and adds the check.
Sources and further reading
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.