How To Make Gui Test

Hi, I’m fairly new to scripting and making gui’s. I’m trying to make a gui that tests the players knowledge about something. It’s basically a multiple choice test. A decal will be shown showing a picture and the player must either pick A B C or D. There are four questions. Once all of the questions have been answered a gui will appear showing their score out of four. How do I make it so each question appears randomly and not in a certain order.

Thanks for reading.

4 Likes

You can use math.random() to achieve this

2 Likes

Right you could put the questions in an array and do like math.random(1, #array)

1 Like

When you say number array do you mean the highest number I want it to pick?

1 Like

Do I put the script as a local script in the screen GUI?

Well sort of. #array means the length of the array. So for example if array is:

array = {question1, question2, question3, question4}

randomQuestion = array[math.random(1, #array)] <-- this would be a way to get a random question

Kinda out of the blue, but you should have another table with the question numbers you have already gave the player to do a debounce test if they already received it them reroll, not doing this will result in possible questions being re-given to the player.

2 Likes

yes, put it in a local script in the ScreenGui

1 Like

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 :slight_smile:

3 Likes

What do you mean when you put

for _ = 1

This video explains it better.

1 Like

I was just watching this series today but didn’t get to this vid yet, should I go straight to this vid in the series and skip the other ones or watch the other ones first? I’m like 2 vids behind.

I suggest watching the past ones.

1 Like
  • for is the loop indentifier.
  • _ is a replacement variable, to replace i. If you don’t want to reference a variable, put _.
  • 1,NumberOfQuestions means to repeat the process in the loop NumberOfQuestions times.
1 Like

I know how to use for loops, but how would you use it in this case?

I sended you that video, in reply to the question you got with iGottic.

Do I put the tables containing the questions and answers in the screen gui in the same script that gets random questions? Or should I put them into different scripts?

I think the same one.

I didnt understand too much what you meant, i assume you meant you asked if you should put the tables containing the question and answer in one script, or one script for every question…

1 Like