I have a module script with a function that is supposed to randomly pick a question from a dictionary that is inside the same module script, but i don’t know how to do such. Any help would be appreciated! Thanks!
You could show your dictionary too
Can you show how the table looks like? Do you use strings
as keys, or do you use numbered indexes
? The solution can vary based on the structure of your questions table.
I use strings as keys. Every question is a smaller dictionary containing five items, all with the same names from their correspondant in each other question: command, which is the question text itself; answers 1, 2 and 3, being all incorrect answers; and the correct answer
It looks something like this:
local questions = {
local question1 = {
command = "what is a mitochondria?"
ans1 = "a group of cells"
ans2 = "the core of a cell"
ans3 = "an organelle that exclusively produces enzymes"
correctAns = "an organelle that produces energy for the cell, using oxygen"
}
}
Sorry, while you were replying I was doing fun stuff xD
I would try something like this:
The module
local module = {}
module.Questions = {
{Q = "First moon landing date?", Answ = {A = "Never earth is flat", B = "Today", C = "Yesterday"}, Correct = "A" },
{Q = "Second moon landing date?", Answ = {A = "Never earth is a hamburger", B = "Today", C = "Yesterday"}, Correct = "B" }
}
module.ChooseQuestion = function()
local randInt = math.random(1, #module.Questions)
warn("Question:", module.Questions[randInt]["Q"])
warn("Posible answers:", module.Questions[randInt]["Answ"])
for k, an in pairs(module.Questions[randInt]["Answ"]) do
warn(k, an)
end
warn("Correct Answer:", module.Questions[randInt]["Correct"])
end
return module
A script calling the module
local module = require(script.Parent:WaitForChild("ModuleScript")) -- connection for the module
module.ChooseQuestion()
And the output when calling for a question:
Firstly the way you ordered the table is a little wrong. Here is how I would suggest doing it, and also how you can pick a random question:
local questions = {
[1] = {
command = "what is a mitochondria?",
ans1 = "a group of cells",
ans2 = "the core of a cell",
ans3 = "an organelle that exclusively produces enzymes",
correctAns = "an organelle that produces energy for the cell, using oxygen",
},
[2] = {
command = "what is a citocondria?",
ans1 = "a group of cells",
ans2 = "the core of a cell",
ans3 = "an organelle that exclusively produces enzymes",
correctAns = "an organelle that produces energy for the cell, using oxygen",
},
}
local randQuestion = questions[math.random(1,#questions)]
print(randQuestion)
Afterwards, you can access the different keys as follows:
print(randQuestion.command, randQuestion.correctAns)
Thanks! i will be doing that when i have time to lol
Your solution seems interesting too, i might take a look into it later as well
I think that embedded tables gives you more room to store and handle more stuff, getting all possible answers, the question, the correct one using a key
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.