Table.insert doesn't work as expected

I’m trying to add something to a table by doing this:

table.insert(table2["Inventory"], ID) -- ID = 4

But my table would look like:

table2 = {
    Inventory = {
        [1] = 4 -- It says [1] = 4, but how do I make it only say 4?
    },
}

It is inserting the value 4 into the first available position of the array, which in this case is 1.

1 Like

I’m not sure if you mean that as the key or the value. If you mean as the value, it actually is the same thing in a different syntax. If you mean it as a key, you can use:

table.insert(table2["Inventory"], ID,Value)
1 Like

[1] is the index of the value in the table, it is the index where 4, the value is present.

You can access it by table2.Inventory[1]. If you want to insert 4 on the 4th index you can do table2.Inventory[ID] = ID where ID is 4.

1 Like