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

Inserting a dictionary in a table?

Is there a direct way to insert a dictionary into a table to reference back to. EX:

local MyTable = {}

table.insert(MyTable, 1, key = 1)

--clearly wont work. but i just wanted to know if there was a good way to insert a dictionary into a table

1 answer

✓ Accepted answer

Adding a dictionary to a table can be done by indexing the table with the key:

local MyTable = {} --New table reference
MyTable.key = 1

print(MyTable.key) --Prints 1 in output

table.insert() is used to add a value to an array. An array is a numerically-indexed table. It would look like:

local array = {11, 12, 13, 14}

print(array[3]) --Would print 13 in the output.

You don't use table.insert() to add a key-value pair to a table.


#####Let me know if you need me to explain something or I had missed anything.

###Tables

Edit:

You would give an index in the existing table to reference a new table. Then you use the previous method to set any keys with their values.

local myTable = {
	someKey = 3
}

myTable.newTable = {} --construct table in existing table under an index
myTable.newTable.Key = 1 --Same as my answer before

print(myTable.newTable.Key) --Prints 1 to output

1 comment

0

This was a very well constructed answer and has bettered my understanding of dictionaries, ty.

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