Quiz System For Trainee Application returns an Error

Hey guys!

I’ve been commissioned to make a trainee application system but I came across an error:

Players.SilentDevReal.PlayerGui.Client:171: attempt to index nil with 'Answer'  -  Client - Client:171

Line 171: if v.TextLabel.Text == QuestionsAndAnswers[CurrentQuestion].Answer then

Script:

local StartButton = script.Parent.Main.Background.StartButton
local AnswerList = script.Parent.Main.Background.ApplicationFormRelated.AnswerList
local QuestionsList = script.Parent.Main.Background.ApplicationFormRelated.QuestionsList
local QuestionText = script.Parent.Main.Background.ApplicationFormRelated.Question

local ProggressBar = script.Parent.Main.Background.ProggressBar
local Bar = ProggressBar.Bar

local Proggress = script.Parent.Main.Background.Proggress
local Initialization = Proggress.Initialization
local ApplicationForm = Proggress.ApplicationForm
local Confirmation = Proggress.Confirmation

local CurrentStageText = script.Parent.Main.Background.CurrentStage
local ConfirmationText = script.Parent.Main.Background.ConfirmationText

local QuestionsAndAnswers = {
	["Why are you interested in the trainee position?"] = {
		Answer = "To assist every player and ensure they have a positive experience.";
		Options = {
			["A"] = "To enjoy the game!",
			["B"] = "To cause mischief",
			["C"] = "To assist every player and ensure they have a positive experience.",
			["D"] = "To pass the time without any specific goals."
		}
	};
	["What unique qualities can you bring to our game?"] = {
		Answer = "A helpful and kind attitude towards all players.";
		Options = {
			["A"] = "A helpful and kind attitude towards all players.",
			["B"] = "Nothing in particular",
			["C"] = "A knack for causing chaos",
			["D"] = "A tendency to ignore players and act rudely."
		}
	};
	["How would you handle a player trolling in the game?"] = {
		Answer = "Report to a higher authority within the game.";
		Options = {
			["A"] = "Ignore them.",
			["B"] = "Join them in their mischief.",
			["C"] = "Report to a higher authority within the game.",
			["D"] = "Start sending help messages."
		}
	};
	["How can you assist a player in need?"] = {
		Answer = "By maintaining a positive and respectful attitude";
		Options = {
			["A"] = "By maintaining a positive and respectful attitude",
			["B"] = "By disregarding their needs",
			["C"] = "By responding with spam and troll messages",
			["D"] = "By refusing to offer assistance"
		}
	};
	["What actions would you take if a player is unhappy with an aspect of the game?"] = {
		Answer = "Attempt to resolve their issue and ensure their satisfaction with the game.";
		Options = {
			["A"] = "Argue with them and insist they are wrong.",
			["B"] = "Attempt to resolve their issue and ensure their satisfaction with the game.",
			["C"] = "Respond with spam and troll messages.",
			["D"] = "Disregard their concerns."
		}
	};
	["What should you do if a higher-ranking member is not fulfilling their duties correctly?"] = {
		Answer = "Report to the game's ownership as soon as possible while avoiding conflict.";
		Options = {
			["A"] = "Ignore their behavior.",
			["B"] = "Report to the game's ownership as soon as possible while avoiding conflict.",
			["C"] = "Start sending HELP messages.",
			["D"] = "Attempt to fulfill their duties in their stead."
		}
	};
	["How would you react if a mid-ranking member is disrespectful towards a player?"] = {
		Answer = "Report to a higher-ranking member as soon as possible.";
		Options = {
			["A"] = "Join them in their disrespect.",
			["B"] = "Ask them to stop their behavior.",
			["C"] = "Report to a higher-ranking member as soon as possible.",
			["D"] = "Disregard their behavior."
		}
	};
	["What actions would you take if there are spammers in the game?"] = {
		Answer = "Report to a higher-ranking member or senior management.";
		Options = {
			["A"] = "Report to a higher-ranking member or senior management.",
			["B"] = "Join them in spamming.",
			["C"] = "Try to understand their motives.",
			["D"] = "Ignore their behavior."
		}
	};
	["How would you react if a senior management or ownership member is in the game?"] = {
		Answer = "Continue with your duties.";
		Options = {
			["A"] = "Approach them and spam greetings.",
			["B"] = "Continue with your duties.",
			["C"] = "Start panicking.",
			["D"] = "Attempt to impress them with your performance."
		}
	};
	["If you decided to quit, how would you handle the situation professionally?"] = {
		Answer = "Act as professionally as possible.";
		Options = {
			["A"] = "Act as professionally as possible.",
			["B"] = "Cause chaos before leaving",
			["C"] = "Announce your departure with spam messages",
			["D"] = "Leave without any announcement or explanation."
		}
	};
}



Initialization.TextColor3 = Color3.fromRGB(55, 122, 229)

local CurrentQuestion = 0

function getNextQuestion(currentQuestion)
	local keys = {}
	
	for k in pairs(QuestionsAndAnswers) do
		table.insert(keys, k)
	end
	
	table.sort(keys)
	
	for i = 1, #keys do
		if keys[i] == currentQuestion then
			return keys[i + 1]
		end
	end
	
	return nil
end

function getCurrentQuestion(currentQuestion)
	local keys = {}

	for k in pairs(QuestionsAndAnswers) do
		table.insert(keys, k)
	end

	table.sort(keys)

	for i = 1, #keys do
		if keys[i] == currentQuestion then
			return tostring(i)
		end
	end

	return nil
end

StartButton.TextLabel.MouseButton1Click:Connect(function()
	StartButton.Visible = false
	for i, v in pairs({AnswerList, QuestionsList, QuestionText}) do
		v.Visible = true
	end
	Initialization.TextColor3 = Color3.fromRGB(97, 97, 97)
	QuestionsList["1"].TextLabel.TextColor3 = Color3.fromRGB(55, 122, 229)
	ApplicationForm.TextColor3 = Color3.fromRGB(55, 122, 229)
	CurrentQuestion = 1
	local CurrentQuestion = "Why are you interested in the trainee position?"
	for i, v in pairs(AnswerList:GetChildren()) do
		if v:IsA("Frame") then
			v.TextLabel.Text =  QuestionsAndAnswers[CurrentQuestion]["Options"][v.Name]
		end
	end

	for i, v in pairs(AnswerList:GetChildren()) do
		if v:IsA("Frame") then
			v.TextLabel.MouseButton1Click:Connect(function()
				if v.TextLabel.Text == QuestionsAndAnswers[CurrentQuestion].Answer then
					-- Correct answer was clicked
					QuestionsList[getCurrentQuestion(CurrentQuestion)].TextLabel.TextColor3 = Color3.fromRGB(97, 97, 97)
					CurrentQuestion = getNextQuestion(CurrentQuestion)  -- You need to define this function
					if CurrentQuestion then
						QuestionsList[getCurrentQuestion(CurrentQuestion)].TextLabel.TextColor3 = Color3.fromRGB(55, 122, 229)
						for i, v in pairs(AnswerList:GetChildren()) do
							if v:IsA("Frame") then
								v.TextLabel.Text =  QuestionsAndAnswers[CurrentQuestion]["Options"][v.Name]
							end
						end
					else
						-- No more questions, change the color of Confirmation
						Confirmation.TextColor3 = Color3.fromRGB(55, 122, 229)
					end
					else print('debug', v.TextLabel.Text)
				end
			end)
		end
	end

end)

I’ve tried different ways to solve it but none worked and now I don’t know what else could be the problem of the error.

Line 171: if v.TextLabel.Text == QuestionsAndAnswers[CurrentQuestion].Answer then

That error is relating to QuestionsAndAnswers[CurrentQuestion].Answer
but more specifically, QuestionsAndAnswers[CurrentQuestion] being nil

This means that whatever CurrentQuestion is, it does not exist in the table QuestionsAndAnswers when this error comes out

1 Like

Yeah, I understood that. However, I don’t know why it’s nil…

1 Like

Well you could check it out, print out QuestionsAndAnswers in the output and check out what does exist inside of the table QuestionsAndAnswers. You should be able to find a pattern because you wrote the code related to what you expected to see and what is actually inside of QuestionsAndAnswers.

QuestionsAndAnswers exist but the nil is returned from the function getCurrentQuestion.

Guys, I solved it with a different method, thanks for your interest! @Sub2_AlteredMindYT

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.