I want to create an array in Lua to store questions and four answers in a quiz game. So arrays in Lua are called Tables right? I know that they can be iterated over but I've not seen one where you can have multiple 'columns' and 'rows', not just indexes and values. Is there a way to create a 2-dimensional array/table in Lua?
I'm not entirely sure what you mean.
You can have regular tables, which should be sufficient for a quiz game.
local myTable = {"hi", 1, 5, true, "bye"}
You can have tables within tables, which i have always thought were kind of messy.
local myTable = {{"hi","bye"}, {1,2}, {true,false}}
If you want to be able to easily find the correct answer to any question, you could simply make the indexes correlate.
local questions = { "2 + 2 = ?", "5 + 5 = ?", "red + blue = ?" } local answers = { "4", "10", "purple" }
This way answers[1]
is the answer to questions[1]
. You can find the answer to any question by simply using the same index in the opposite table.
Please feel free to ask questions, I was kind of confused.
EDIT:
The easiest way to do that would be to use instances. Although I'm hesitant to say it's impossible to do with tables, it would probably be cleaner to use instances. You could make it simply empty Models with the Name property storing the question and answer, or you could use StringValues and have the value property store the questions and answers. It would look something like this in the Explorer:
Questions Question1 Answer1 Answer2 Answer3 Answer4