To explain from square one, a table is just a list of values. In Luau(Roblox’s version of the Lua programming language), you can put any value in it, and can have different types of values in the same table. I assume you already know all this.
local t = {5, false, 67, {1, 2}, Vector3.new(5, 5, 5)}
Let’s say I want to access the third value in the table.
I’d do this:
local value = t[3]
The value
variable represents the third element/value in the table, which is 67, right? Pretty straightforward stuff.
A dictionary is a table, except when you want to access a value, you don’t use a number, and instead you use some other data type. Most commonly people use the string type because it allows you to mimic an actual dictionary:
local d = {
Roblox = "An online game",
Polygonizised = "The OP"
}
print(d.Roblox) -- Prints "An online game"
You’re basically giving each value in the table a name. As you can see above, if I wanted to get the definition of the word “Roblox” I can just type the variable holding the dictionary, then a dot, and then the name of the key whose value I want to retrieve.
You can also set up string keys like this:
local d = {
["Roblox"] = "An online game",
["Polygonizised"] = "The OP"
}
It does the exact same thing either way.
An example scenario of when you’d use a dictionary would be when you’re saving player data. Say they have a level, a certain amount of exp, skins in their inventory, cash, etc.
Instead of setting it up like this, where you’d have to remember what numeric index points to what:
local d = {
{5, 1000},
{"Skin1", "Skin2"},
500
}
print(d[1]) -- Prints {5, 1000}. What? What is the value at index 1 supposed to represent?
You can set it up like this, where you don’t have to memorize the location of certain data:
local d = {
Levels = {
Level = 5,
Exp = 1000
},
Skins = {
"Skin1",
"Skin2",
},
Cash = 500
}
print(d.Levels.Level) -- Oh, it's clear that this is printing the level of the player!
Now, in your case, I assume you don’t really care where to find each question within your questions
table, just that when you access one, you can easily type question.Question
to get the question, and question.Answers
to get the table of answers for that question. The way you have each question set up is perfectly fine, it’s just that you’re giving a name to each question, which isn’t really necessary and would make it more difficult if you wanted to randomly select a question from the list of questions, which is why I changed your code in my earlier post.
A dictionary can contain tables, and a table can contain dictionaries. Both can contain both. It’s quite flexible. You don’t have to make a table a dictionary in order for it to contain dictionaries.