Why are the sub-tables of this table not being recognized?

So I have a dictionary for storing asset IDs and BodyColors returned by the Players:GetCharacterAppearanceInfoAsync() function:

local Data =
{	
	Assets =
	{
		Hats = {},
		TShirts = {},
		Shirts = {},
		Pants = {}
	},
	
	BodyColors =
	{
		Head = {},
		Torso = {},
		LeftArm = {},
		RightArm = {},
		LeftLeg = {},
		RightLeg = {}
	}	
}

Running print(data) prints in the output
table: 00000200066DBCB0, which is expected behavior.
But when I try to print(data[1]), theoretically, I should be having another table print because table[1] would lead to the Assets subtable, but instead I get nil printed.

Here’s the actual ModuleScript the data is contained in (this isn’t related to DataStore):

-- Module to store data.

local DataStorage = {}
local MarketplaceService = game:GetService("MarketplaceService")

local Data =
{
	Assets =
	{
		Hats = {},
		TShirts = {},
		Shirts = {},
		Pants = {}
	},
	
	BodyColors =
	{
		Head = {},
		Torso = {},
		LeftArm = {},
		RightArm = {},
		LeftLeg = {},
		RightLeg = {}
	}
}

return DataStorage

Here’s the result of printing this table:

print(Data, Data[1])
> table: 00000200066DBCB0 nil

Any help? Am I not understanding dictionaries correctly? I looked on the wiki for tables and it doesn’t seem to highlight this particular issue. I also tried enclosing the sub-table names with brackets and quotes, but that netted the same result. Is it because there are no keys inside of the sub-tables?

You are indexing it incorrectly, here try this

2 Likes

This is due to the fact that 1 is not the key.

Look at the following examples.

local t = {
    {"thing","thing2"}
}

Printing t[1] would print the memory address, as expected. However…

local t = {
    subtable = {"thing","thing2"}
}

Printing t[1] in the above example would come out to nil because the key isn’t actually 1, it’s subtable. Printing out t.subtable or t["subtable"] would give you the sub table.

3 Likes

Ah. Thanks.

1 Like

No problem. I’d recommend looking at the Lua tutorial on tables for more information on arrays (keys ascending from 1) and dictionaries (string keys).

2 Likes