Adding items to to a table

I’m trying to build a base for an inventory system, and my method is to create a table in a datastore for the player with the names/ids of the items owned by the player.

Here’s what I’ve got so far.

local data = {
["Inventory"]  = {0000000,00000,0000,00000,0000}
}

How would I add values to this ["Inventory"] table, without overwriting previous values?

1 Like

Roblox has a built in library of functions for tables. One of these functions is table.insert() which allows for values to easily be added to a table.

https://developer.roblox.com/en-us/api-reference/lua-docs/table

7 Likes

There are several valid ways of doing this!

There is no shame in doing it the simple way sometimes too!

All of these methods work:

MyTable[#MyTable+1] = Malue
table.insert(MyTable, #MyTable+1, Value)
table.insert(MyTable, Value)
3 Likes