Storing questions, wrong answers, and correct answer

I’m creating a game where you try to answer questions as fast as possible to get to the finish first. The questions are multichoice, along with a few short answers. I want to be able to store the correct and wrong answers to a question. How would I go about doing this? Is this possible to do in a dictionary? or will I need to create a folder with all the questions and answers?

Example:
Question: How old is Bill Gates
Wrong answers: 55, 42, 13
Correct answer: 66

here’s an idea:

let questions = {
    ["How old is bill gates?"] = {
        ["55"] = false,
        ["42"] = false,
        ["13"] = false,
        ["66"] = true,
    }.
}

How would I go about getting a random question through a script, and also randomizing the answers?

You can use math.random and pick a number between 1 and the amount of questions in the “questions” table.

True, something like this

local Random = Questions[math.random(1, #Questions)]
print(Questions[Random])

And for randomizing the answer choices?

Not sure why you would want to randomize the answer of a question, explain further.

I think he means to randomize where the answers are located:

Example:
Answer 1, Answer 2,Answer 3,Answer 4
Randomized:
Answer 2, Answer 1,Answer 4,Answer 3

Yeah, this is what I meant by randomize ^

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:

image

2 Likes


image

Oops! Here is correction:

function Utils.getCorrectAnswer(choices)
	for answer, choice in choices do
		if choice == true then
			return answer
		end
	end
end
1 Like

How can I also make it so that I can pick a random question and not have them repeat in the same round?

It prints nil. This is the table:

local questions = {
	["How old is bill gates?"] = {
		["55"] = false,
		["42"] = false,
		["66"] = true,
		["33"] = false,
	},
	["What year was Roblox created?"] = {
		["2003"] = false,
		["2006"] = true,
		["2013"] = false,
		["2018"] = false,
	},
}

Its an example, dont take it seriously

Even if I do questions[1] it prints nil, how can I fix this?

I Believe you do this:

questions["How old is bill gates?"]

So how would I pick a random question from the list then?

Maybe Something like this:

local Questions = {
Question1 = {
Question = "How old is Bill Gates?";
[1] = ["2003"] = false;
[2] = ["2006"] = true;
[3] = ["2013"] = false;
[4] = ["2018"] = false;
};
}

Didn’t work. It still prints nil.