How can I access and read a dictionary inside a dictionary?

I would like to access a dictionary filled with information that is also in another dictionary, but im not sure how to access the dictionary inside the dictionary.
here is my attempt:

local building = {}


local partinfo = {
	name = script.Parent.Name
	}


building["partinfo"] = {partinfo}



local part = building["partinfo"]
local info = part["name"]
print(info)

when I print “info”, it prints nil when its expected to print “Part” as that’s the name inside the dictionary “partinfo”.

2 Likes

Change name into a dictionary.

["name"] = script.Parent.Name

Er actually, your whole thing is a table and not a dictionary to begin with. I made a similar mistake before.

Post: Module Help, not printing things inside of table - #4 by kingdom5

This should work:

local building = {}


local partinfo = {
	name = script.Parent.Name
	}


building["partinfo"] = {partinfo}



local part = building["partinfo"]
local info = part[1]["name"]
print(info)

The problem is that you put the table in another table (array specifically), and you’d need to index it properly.

The table’s format would look like this:

building = {
       ["partinfo"] = {
            {
                  name = script.Parent.Name
            }
       }
}
2 Likes

This is happening because partinfo itself is a table, and you’re putting it inside a table, turn this

building["partinfo"] = {partinfo}

To this

building["partinfo"] = partinfo

When you do part["name"] you’re searching for a key named “name” inside of the table partinfo is stored in, aka this one {partinfo}

2 Likes

I tested raret’s script and it works.

They should both work really
¯\ _ (ツ)_/¯

Also this entire script is quite a mess, it can be easily reduced to something like this

local building = {}

building.partinfo = {name = script.Parent.Name}

local part = building.partinfo --this is the part
local name = part.Name --this is the name info
print(name)

Doing

building.part = something

Is same as

building["part"] = something

oh ok, im trying to setup datastores that use dictionaries to index information about a model full of parts, so Im learning as much about tables and dictionaries as I can.

This question has been answered before. Please search before posting.

https://devforum.roblox.com/search?q=help%20table%20category%3A54