Both will work, although I personally prefer to store data in dictionary whenever possible.
local questions = {
["How old is bill gates?"] = {
["55"] = false,
["42"] = false,
["13"] = false,
["66"] = true
}
}
And then you can check if the answer is correct.
--[[
This function will go through each
entry until it finds a true value
and will return corresponding key,
which will be the answer.
]]
local function getCorrectAnswer(choices)
for answer, choice in choices do
if choice == true then
return answer
end
end
end
local currentQuestion = "How old is bill gates?"
local playerAnswer = "66"
if getCorrectAnswer(questions[currentQuestion]) == playerAnswer then
print("Correct!")
else
print("Wrong!")
end
To randomly pick a question you can use math.random(min, max)
.
--[[
We cannot get the length of dictionary
where keys are named. This function will
fix this problem by counting each entry
and returning the total count.
]]
function getCountOf(entries)
local count = 0
for _, _ in entries do
count += 1
end
return count
end
local randomQuestionIndex = math.random(1, getCountOf(questions))
And to randomize the order of choices you can use Fisher-Yates shuffle algorithm.
-- Fisher-Yates shuffle implementation
local function shuffle(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
end
--[[
We cannot get named entries by index,
this function will fix this problem by
going through each entry and keeping track
of a position until it reaches desired position
which will correspond to an entry.
]]
local function getEntryByIndex(entries, index)
local pos = 0
for _, entry in entries do
pos += 1
if pos == index then
return entry
end
end
end
local choices = getEntryByIndex(questions, randomQuestionIndex)
shuffle(choices) -- Randomize order of choices
Entire code
local questions = {
["How old is bill gates?"] = {
["55"] = false,
["42"] = false,
["13"] = false,
["66"] = true
}
}
--[[
This function will go through each
entry until it finds a true value
and will return corresponding key,
which will be the answer.
]]
local function getCorrectAnswer(choices)
for answer, choice in choices do
if choice == true then
return answer
end
end
end
local currentQuestion = "How old is bill gates?"
local playerAnswer = "66"
if getCorrectAnswer(questions[currentQuestion]) == playerAnswer then
print("Correct!")
else
print("Wrong!")
end
--[[
We cannot get the length of dictionary
where keys are named. This function will
fix this problem by counting each entry
and returning the total count.
]]
function getCountOf(entries)
local count = 0
for _, _ in entries do
count += 1
end
return count
end
local randomQuestionIndex = math.random(1, getCountOf(questions))
-- Fisher-Yates shuffle implementation
local function shuffle(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
end
--[[
We cannot get named entries by index,
this function will fix this problem by
going through each entry and keeping track
of a position until it reaches desired position
which will correspond to an entry.
]]
local function getEntryByIndex(entries, index)
local pos = 0
for _, entry in entries do
pos += 1
if pos == index then
return entry
end
end
end
local choices = getEntryByIndex(questions, randomQuestionIndex)
shuffle(choices) -- Randomize order of choices
If you want the code to look a bit less cluttered with util functions, you can move them to a ModuleScript and then require it.
Main
local Utils = require(script.Utils)
local questions = {
["How old is bill gates?"] = {
["55"] = false,
["42"] = false,
["13"] = false,
["66"] = true
}
}
local currentQuestion = "How old is bill gates?"
local playerAnswer = "66"
if Utils.getCorrectAnswer(questions[currentQuestion]) == playerAnswer then
print("Correct!")
else
print("Wrong!")
end
local randomQuestionIndex = math.random(1, Utils.getCountOf(questions))
local choices = Utils.getEntryByIndex(questions, randomQuestionIndex)
Utils.shuffle(choices) -- Randomize order of choices
Utils
local Utils = {}
--[[
We cannot get named entries by index,
this function will fix this problem by
going through each entry and keeping track
of a position until it reaches desired position
which will correspond to an entry.
]]
function Utils.getEntryByIndex(entries, index)
local pos = 0
for _, entry in entries do
pos += 1
if pos == index then
return entry
end
end
end
-- Fisher-Yates shuffle implementation
function Utils.shuffle(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
end
--[[
We cannot get the length of dictionary
where keys are named. This function will
fix this problem by counting each entry
and returning the total count.
]]
function Utils.getCountOf(entries)
local count = 0
for _, _ in entries do
count += 1
end
return count
end
--[[
This function will go through each
entry until it finds a true value
and will return corresponding key,
which will be the answer.
]]
function Utils.getCorrectAnswer(choices)
for answer, choice in choices do
if choice == true then
return answer
end
end
end
return Utils
So it will look like this: