Weird "attempt to index nil value" error (SOLVED)

I’m getting this error on a module containing some level data, resulting in a bunch of other errors in the script that requires it:

ERROR:SCRIPT: /zones/test_level/test_level.lua:3: attempt to index a nil value
stack traceback:
	/zones/test_level/test_level.lua:3: in main chunk
	[C]: in function 'require'
	/sonic/sonic#.script:104: in function </sonic/sonic#.script:100>

The weird thing is that this module worked perfectly fine before, and I haven’t touched it in some time. There aren’t any other errors above it in the console, so I can’t tell why this is happening. Here’s the module code:

local M = {}

M[1][0] = 16
M[1][1] = 16
M[1][2] = 16
M[1][3] = 16
M[1][4] = 16
M[1][5] = 16
M[1][6] = 16
M[1][7] = 16
M[1][8] = 16
M[1][9] = 16
M[1][10] = 16
M[1][11] = 16
M[1][12] = 16
M[1][13] = 16
M[1][14] = 16
M[1][15] = 16

M[1]["ang"] = 0

return M

I find it even more weird that you say it worked in the first place.

You are creating a table and putting it in M
M = {}
But then the table contains nothing (nil) so trying to index another table within that table is impossible as it hasnt been created.
M[1] = nil

5 Likes

topbraj on Discord got the answer first :stuck_out_tongue:

1 Like

I think that’s supposed to say "thank you for the answer, @andreas.strangequest ", @Potota

6 Likes

Not really sure why you are doing a multiple array in the first place for this module…
There is a Discord server?

Yes! You’re more than welcome to join! https://discord.gg/cHBde7J

Just a question regarding the error message, but I guess it is more regarding Lua than Defold, but maybe: can it point out immediately which value actually is trying to be indexed, but is nil?

1 Like

Not sure which function to override, but it sound like a case where you’d like to monkey patch a function, and printout your own information.

I mean a general use-case for debugging such message is indeed printing out the values, but I dreamed about it to be done for me in this log :sweat_smile: because, you know, sometimes I have in on line: if table1[i] and table2[i] and table3[i] and so on in one line and is taking sooooo much time to print them all :smiley: jk I don’t think there’s monkey patch solution for conditions, right?