Lua tables question (SOLVED)

Hi - I want to store data for my game in Lua tables but they’re not working. I’m storing the data in modules so it’s easy to reuse across components. An example would look like this:

local M = {
	1 = { 1 },
	2 = { 1, 1 },
	3 = { 2, 1 },
	6 = { 1, 2, 3 },
	10 = { 2, 3 },
}

return M

This shows the information for the enemy waves in my game. The indices refer to the wave, and the values are the enemies that will appear in that wave.

Note that I skip indices, they are not sequential.

When I try to run this I get the error:

/data/waves.lua
	Line 2: Compilation failed: '}' expected (to close '{' at line 1) near '='

What have I done wrong?

Thanks!

Ha ha - I just found the answer after posting this. The problem was using numbers as the keys. If I wrap the numbers in square brackets the code runs just fine.

local M = {
	[1] = { 1 },
	[2] = { 1, 1 },
	[3] = { 2, 1 },
	[6] = { 1, 2, 3 },
	[10] = { 2, 3 },
}

return M
2 Likes

Nicely solved. And welcome to the forum! I thought I recognized the avatar.

2 Likes