That's because you're doing it wrong. Here's what your code is currently doing:
Code:
mounts = {}
category="new_category"
mount="Swift Moonsaber"
- The first line creates the table { } and assigns it to the variable "mounts".
- The second line creates the string "new_category" and assigns it to the variable "category".
- The third line creates the string "Swift Moonsaber" and assigns it to the variable "mount".
Now when you do:
Code:
mounts[category][mount]=true
This is what's happening:
- The code looks for a table under the variable name "mounts". This returns the table, {}.
- The code looks for the value under the variable name "category" as a table key. This returns nil, because the "mounts" table is empty.
- The code looks for the value under the table returned by "mounts[category]". This fails and throws an error, because "mounts[category]" is nil, not a table.
What you need to do is:
Code:
local mounts = {}
local category = "new_category"
local mount = "Swift Moonsaber"
mounts[category] = {}
Then you can add values to the category the way you were trying to:
Code:
mounts[category][mount] = true
On a side note, you should
never create global variables with such generic names as "mounts" and "category" and "mount". Such variables should
always be declared as local. If you need those values to be accessible from other files, use your addon's private namespace (best) or give them
distinct global names if absolutely necessary (last resort), such as "GimbolinoMounts" and "GimbolinoCategory". However, the only reasons to put anything in the global namespace are (1) it's a saved variable, or (2) you expect a need for it to be easily accessed from
other addons, or (3) it's your main addon frame/table and you need to be able to easily access it in-game via "/run" commands for now for debugging purposes. If none of those apply, don't make it global.