Help with understanding Empty Tables

Hi, I’ve been trying to learn datastores for a while now but I always get confused when I see people make empty tables such as:

- This is a screenshot from a video by B Ricey titled “Advanced Datastore Tutorial”

From what I got from the video is that he made a separate empty table inside of the module scripts table but put nothing in the table itself.

The “sessionData” table (highlighted) is responsible for holding a players data. My question is how does the data get there without a table.insert? I’ve seen empty tables being used quite frequently but I do not understand how they work and how I can utilize them myself.

If anyone would be able to explain to me how to use these tables and how they work, I would greatly appreciate that because I was not able to find specific information about these anywhere.

4 Likes

Empty tables are created as easily as {}. Now, if you want to initialize the values of that table there are methods such as table.create to do that for you. Also, table.insert is to insert elements into the table, not necessarily to initialize it. In terms of how does one just insert values into the table without it being initialized is as simple as Lua just handles it for you. Tables are dynamically created as in it can increase the memory it needs the more you put in it then decrease itself the more you take out.

5 Likes

There is more information on tables in this article.

I know it can be confusing when you think of other languages when you have to statically or pre-allocated memory for tables, but that’s not necessary for Roblox Lua.

2 Likes

This is because in your example, they are not using a table, but rather a dictionary. The different is that arrays’ indexes/keys are just the simple 1, 2, 3 that are set automatically when a table is created with predefined entries or when something has been added to it, these indexes refer to the value in that position.

But dictionaries can use any type of key, even strings.

Example:

local fruits = {"Apple", "Banana", "Pear"}

for i,v in pairs(fruits) do
    print(i,v)
end

--Expected output
1 Apple
2 Banana
3 Pear

Whereas a dictionary:

local diction = {
    ["Name"] = "Steve",
    ["Food"] = "Apple"
}

for key,value in pairs(diciton) do
    print(key,value)
end

--Expected output
Name Steve
Food Apple

What’s basically happening is when the module is required, it creates an empty sessionData table, and when setupPlayerData is called, it adds a new dictionary entry in the dictionary that contains the player’s userId as the key and a table of what data to store as the value in that dictionary.

Say you have an empty table and setupPlayerData is called on you, and you have 10 money and your userid is 1625. After that call, if you print sessionData, it would give

{
   [1625] = {Money = 10}
}

Assuming that Money in that example is going to be a dictionary key.

And then in ChangeValue, it checks if there’s a valid entry that contains the userid of the player, and if there is, get the dictionary key using the specified name and set its value to whatever is given.

Say you call it using the same player, giving Money as the name and 100 as the value, the line in the if statement woudl look like this

sessionData[1625]["Money"] = 100

Which would do expected, get the key in sessionData using the player’s id, then from that result, get the Money key, and then set its value. The only problem is that there’s no check as of now in that example that check if a valid name was given

The principle works the same if you were to use a table instead of a dictionary, but instead you’d just use table.insert and reference an index, but in this case we need a non incremental index, so a dictionary is needed

4 Likes

This is actually not a Table. but a special type of table called a ‘Dictionary’. Now, to put elements in this Dictionary, you’ll have to do this:

local MyTable = {} -- Assigning table for all our information
-- Let's make a table of some games and their main programming languages
MyTable["ROBLOX"] = "Luau"
MyTable["Minecraft"] = "Java or C++"
MyTable["Cool Math Games"] = "HTML/JavaScript/CSS"

print(MyTable)
-- Will print something like so:
MyTable = {
["ROBLOX"] = {
["Luau"]
},
["Minecraft"] = {
["Java or C++"]
},
["Cool Math Games"] = {
["HTML/JavaScript/CSS"]
}
}
2 Likes

Thank you so much for explaining this step by step, the more I re-read what you said the more I understood and I now completely understand how this dictionary works :slight_smile: :+1:
Thank you once again.

2 Likes

Anytime! If you have anymore issues don’t be afraid to make another post!