First, you want a simplistic way of arranging questions and answers. I personally like to put tables inside tables, and arrange like so:
local questions = {
{StringQuestion,{StringAnswer1,StringAnswer2,StringAnswer3,StringAnswer4},IntCorrectAnswerNumber};
{StringQuestion,{StringAnswer1,StringAnswer2,StringAnswer3,StringAnswer4},IntCorrectAnswerNumber};
{StringQuestion,{StringAnswer1,StringAnswer2,StringAnswer3,StringAnswer4},IntCorrectAnswerNumber};
}
For every question you have a table that contains the following: the question, a table of answers, and the number that identifies the correct answer. Let me break this down.
-
StringQuestion is your question. It is written as
"Question here"
.
-
StringAnswer [1-4] is arranged as table. They are written as `{“My answer 1”,“My answer 1”,“My answer 1”,“My answer 1”}
-
IntCorrectAnswerNumber is the number that tells what the correct answer is. If your answer is the second option, put
2
. If it is the fourth, put 4
. Same for one and three.
To get random questions without reusing them, do this:
local numberCorrect = 0 -- Start with 0 correct
local NumberOfQuestions = #questions -- Get number of questions
for _ = 1,NumberOfQuestions do -- Do this for all the questions
local TempQuestionNumber = math.random(1,NumberOfQuestions) -- Get an unused question
local QuestionData = questions[TempQuestionNumber] -- Get your question data table (the thing that is {StringQuestion,{StringAnswer1,StringAnswer2,StringAnswer3,StringAnswer4},IntCorrectAnswerNumber})
local Question, Answers, CorrectAnswer = unpack(QuestionData) -- Turn it from table into three variables.
-- local Question is your question string.
-- local Answers is a table of answers. Don't randomize this.
-- local CorrectAnswer is the number that is the correct answer.
-- For example, you can get the correct answer from the Answers table by doing this:
-- local CorrectAnswerAsWords = Answers[CorrectAnswer]
table.remove(questions,TempQuestionNumber) -- Make sure question is not re-used.
end
local percentCorrect = math.floor(numberCorrect/NumberOfQuestions * 100) -- What percent of correct answers you got.
If you have questions about this, ask me! I hope I helped 