Can I do this with a table?

Lets say I want to add a name to a table inside of a table…

local aTable = {}

I want to make a dictionary (I believe its what it is called) inside that table. Something like:

local aTable = {
   ['Name'] = {
      -- more stuff
   },
   ['AnotherThing'] = {
      -- more stuff for another thing
   },
}

How would I do that inside a script, not manually.

So for one thing, you assign tables to indexes if you already defined your parent table like this:

aTable[index] = value

index and value can literally be anything. This includes a table, function, etc, as long as it’s not nil:

aTable['Name'] = { 
	-- stuff
}

aTable['AnotherThing'] = {
	-- more stuff
}

There is also a dot syntax for this:

aTable.index = value

That is equivalent to this:

aTable["index"] = value
-- or
aTable['index'] = value -- obviously

So you could rewrite your table definition like this, and it would be literally the same:

aTable.Name = {
	-- stuff
}

aTable.AnotherThing = { --[[ you get the idea ]] }

Scripting this will depend on your implementation, but the basis of generating that table with a script to make cleaner is usually with loops:

local aTable = {}

for i = 1, 20 do
	aTable[i] = {}
end
3 Likes

Thanks for reminding me. I absolutely forgot how tables worked and thank you for your very detailed response! :slight_smile:

1 Like