Help making a table of dictionaries

Hi, devforum! I’m creating a trivia game, and i want to keep the questions and answers in a table inside of another. (e.g Answer = QuestionsTable.Question1.Answer)

How would i do this? This is what i have so far. I have never worked with indexes dictionaries, if that’s what this is called.

local questions = {
	question1 = { Question = "What is the in-game currency used in ROBLOX?",
		Answers = {"robux"}
	}
}
2 Likes

Hello.

You can access these tables by doing questions.question1.Question ect.

If you want to create an automatic system, you can do:

for i=1, 5 do
    TextLabel.Text = questions[question..(i)].Question
    Answer.Text = questions[question..(i)].Answers[i]
end

Output: “What is the in-game currency used in ROBLOX?”
Answer: “robux”

These tables almost work in the same way as accessing children inside a model. (If I said something wrong, feel free to correct me)

Hope this helped! :slight_smile:

2 Likes

Having a dictionary for each question is a pretty good start, but I assume you’re probably going to randomly choose questions, so it’s probably best you don’t use strings as the keys/indexes in the questions table:

local questions = {
	{
        Question = "What is the in-game currency used in ROBLOX?",
		Answers = {"robux"},
	},
    {
        Question = "Who is the CEO of Roblox?",
        Answers = {"david baszucki", "builderman"}
    }
}

local chosenQuestion = math.random(#questions)

It would make it way easier to choose random questions down the road, because if you want to display the question to all the players and check if their answers are correct, you’d do something like this:

print(chosenQuestion.Question)

game.Players.PlayerAdded:Connect(function(player: Player)
    player.Chatted:Connect(function(message: string)
        local isAnswerCorrect = table.find(chosenQuestion.Answers, message:lower()) ~= nil
    end)
end)
1 Like

I’m a little confused by your reply here. Could you go a little more in-depth?

1 Like

can you explain what you want to make? i tried your script and it does what you said

local questions = {
	question1 = { Question = "What is the in-game currency used in ROBLOX?",
		Answers = {"robux"}
	}
}

local question = questions.question1.Question
print(question) -- prints What is the in-game currency used in ROBLOX?
local answer = questions.question1.Answers
print(answer) -- prints [1] = "Robux"
2 Likes

basically just it picks one player to answer the (a randomly selected) question through the chat box, and if its correct, it picks another player.

2 Likes

i made a script and tested it and it work you can modify it to your preferences
it used A recursive function to make a game loop you can change it to while loop if you wanted


local Players = game:GetService("Players")

local questions = {
	question1 = { Question = "What is the in-game currency used in ROBLOX?",
		Answers = {"robux"}
	}
}


local event = nil

local function AskPlayerAQuestion()
	
	local pickedPlayer = Players:GetPlayers()[math.random(1, #Players:GetPlayers())]
	event = pickedPlayer.Chatted:Connect(function(msg)
			if string.lower(msg) == string.lower(questions.question1.Answers[1]) then
			print(`{pickedPlayer.Name} : answered the question`) -- player won
				event:Disconnect()
				AskPlayerAQuestion()
				
			else
				print(`{pickedPlayer} answered wrong answer`) -- player said wrong answer
			end
	end)
	
	
	task.wait(10)
	print("player didnot answer in time picking new one") -- player didnot chat in 10 seconds after the question was asked
	event:Disconnect() -- disconnecting it because it willnot get disconnected automaticly
	AskPlayerAQuestion()
	
end

AskPlayerAQuestion() -- A recursive function


the reason why i added an event thread is to become able to disconnect the event msg event
there are alot other ways of doing it and this isnot the most optimized script
if it gave error that is because the script ran before the player load a temporarily fix is to add task.wait(2) at the top

2 Likes

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.

2 Likes

Thanks for your help! From your earlier post, i learned that i simply didn’t need the question1 = of

question1 = { question = "blah blah blah",
          answer = "i agree" 
}

Thanks again!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.