bad argument #1 to ‘insert’ table expected got nil RedM/CFX/VORP/LUA

What does “SCRIPT ERROR: bad argument #1 to ‘insert’ (table expected, got nil)” mean?

Look at the script at that like you will find something like this:

table.insert(table_name, data_to_insert)

The error means that the varaible that you are passing into the function “table_name” is nil instead of a table.

Causes?

You haven’t declared your variable correctly, so it doesn’t exist. This could be you forgot to create it at all, or it could be you have a timing issue where it is created in a thread elsewhere in your code, and this function is being called to early.

The table_name is being returned as part of a db query or a nui response, and the returned results are nil. In this case you need to look at the functions or nui that are returning data to the lua script. Figure out what they are returning and why it is nil.

By MK

Dragon Codes