"Attempt to index nil" error - Cause & Fix

"Attempt to index nil" error

Hey, Cookie Tech community! I feel like the `Attempt to index nil` error is really common, and so I'd like to explain the [cause](#Cause) of it, and the best ways to fix it.


This error is pretty common, an easy mistake to make. This error comes along normally when you’re running a function, or finding something in something nil. Example:

local tbl = nil 
local f = tbl['hi'] 

tbl is nil, therefore we are “attempting toindexing nil” by finding ‘hi’ in the table.


The easiest fix is to attempt to find what you’re trying to find is. If it’s nil, you’ll likely be getting the error. Find any place where it’s changed, and ensure that the type remains as intended. The most common issue with me is setting a table to a datastore that’s nil. This can be fixed by setting the datastore to an empty table.

I hope this helped! Feel free to leave a reply if you have any comments or concerns.

@cam; Moderator

1 Like