Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to insert Dictionaries into Tables?

So, I have a table that looks like this:

local Mytable ={
["Money"] = 500,
["Inventory"] ={
["Bread"] = 1,
["Apples"] = 40,
  },
}

So i'm trying to insert another Dictionary like this:

["Pets"] ={
["Dogs"] = 1,
["Cats"] = 5,
},

But how would I insert that Dictionary into Mytable? I already tried :

table.insert(Mytable,["Pets"] ={
["Dogs"] = 1,
["Cats"] = 5,
},)

But that didn't work... Anyone know how to do this?

1 comment

0

I don't believe this is possible unless you put it into the script itself.

1 answer

✓ Accepted answer

Great question. As you already know, table.insert() will not work with dictionaries! To add values or definitions to your table, there are 2 ways.

1st way:

local Mytable ={
["Money"] = 500,
["Inventory"] ={
["Bread"] = 1,
["Apples"] = 40,
  },
}

Mytable.Color = "Green"

2nd way:

local Mytable ={
["Money"] = 500,
["Inventory"] ={
["Bread"] = 1,
["Apples"] = 40,
  },
}

Mytable['Color'] = "Green"

Adding values to a dictionary is indexing it, which is practically the same way you define it! Hope I've helped.

2 comments

0

Thank you!

0

Glad I could help.

This discussion is preserved as it was. New answers, comments, and votes are closed.