Suggestions on Answer-Checking System

Hello, I am making a Trivia Game and I need suggestions to achieve a good Answer-Checking System.

About the System?

The Answer-Checking System or ACS has to qualify these requirements to be reliable and smart.

:white_check_mark: = Accomplished
:x: = Uncompleted

  • It should accept multiple answers. :white_check_mark:
  • It should not be whitespace sensitive. :white_check_mark:
  • It should not be case-sensitive. :white_check_mark:
  • It should not get confused with numbers. :x:

Let’s see these requirements in action! :clapper:

Requirement 1

Let’s say there is a question, “The Olympics are held every how many years?”

Expected Correct User Answer: 4, 4 Years, Four, Four Years

The user may type different answers that could be correct, so making an ACS that only sees one answer to be correct would just make the gameplay unintuitive.

How do we fix this?

Code
local function QuestionAnswer(Answer, Answer2, Answer3, Answer4, Question)

	local Answer_Input = Question.Answer["Answer Input"]
	local Answer_Label = Question.Answer["Answer Label"]
	local Quote = Question.Answer.Quote
	local Next_Button = Question.Buttons["Next Buton"]
	local Submit_Button = Question.Buttons["Submit Button"]
	local TryAgain_Button = Question.Buttons["Try-Again Button"]
	local Hint_Button = Question.Question["Hint Button "]
	local ToolTip = Question.Question["Hint Button "]["Tool Tip"]
	
	Answer_Input:GetPropertyChangedSignal("Text"):Connect(function()
		if Answer_Input.Text ~= "" then
			Submit_Button.Visible = true
		elseif Answer_Input.Text == "" then
			Submit_Button.Visible = false
		end
	end)
	
	local SUB_TweenColor = Color3.fromRGB(168, 96, 220)
	local SUB_TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local SUB_Tween = TweenService:Create(Submit_Button, SUB_TweenInfo, {BackgroundColor3 = SUB_TweenColor})
	
	local SUB_TweenColor = Color3.fromRGB(200, 112, 255)
	local SUB_TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local SUB_Tween2 = TweenService:Create(Submit_Button, SUB_TweenInfo, {BackgroundColor3 = SUB_TweenColor})
	
	local Next_TweenColor = Color3.fromRGB(82, 153, 214)
	local Next_TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local Next_Tween = TweenService:Create(Next_Button, Next_TweenInfo, {BackgroundColor3 = Next_TweenColor})
	
	local Next_TweenColor = Color3.fromRGB(98, 180, 255)
	local Next_TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local Next_Tween2 = TweenService:Create(Next_Button, Next_TweenInfo, {BackgroundColor3 = Next_TweenColor})
	
	local TG_TweenColor = Color3.fromRGB(213, 60, 46)
	local TG_TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local TG_Tween = TweenService:Create(TryAgain_Button, TG_TweenInfo, {BackgroundColor3 = TG_TweenColor})
	
	local TG_TweenColor = Color3.fromRGB(255, 78, 55)
	local TG_TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local TG_Tween2 = TweenService:Create(TryAgain_Button, TG_TweenInfo, {BackgroundColor3 = TG_TweenColor})
	
	
	Submit_Button.MouseEnter:Connect(function()
		Submit_Button.Font = "GothamBold"
		SUB_Tween:Play()
	end)
	
	Submit_Button.MouseLeave:Connect(function()
		Submit_Button.Font = "GothamSemibold"
		SUB_Tween2:Play()
	end)
	
	TryAgain_Button.MouseEnter:Connect(function()
		TryAgain_Button.Font = "GothamBold"
		TG_Tween:Play()
	end)

	TryAgain_Button.MouseLeave:Connect(function()
		TryAgain_Button.Font = "GothamSemibold"
		TG_Tween2:Play()
	end)
	
	Next_Button.MouseEnter:Connect(function()
		Next_Button.Font = "GothamBold"
		Next_Tween:Play()
	end)

	Next_Button.MouseLeave:Connect(function()
		Next_Button.Font = "GothamSemibold"
		Next_Tween2:Play()
	end)
	
	Submit_Button.MouseButton1Click:Connect(function()
		ClickSFX:Play()
		Hint_Button.Visible = false

		local whitespace_remove = Answer_Input.Text:gsub(" ","")	
		local Input_Lower = string.lower(whitespace_remove)

		if Input_Lower:match(Answer) or Input_Lower:match(Answer2) or Input_Lower:match(Answer3) or Input_Lower:match(Answer4) then
			CorrectSFX:Play()
			Answer_Label.Text = Answer_Input.Text.." ✅"
			Answer_Label.Visible = true
			Answer_Input.Visible = false
			Quote.Text = "“Nothing succeeds like success.”"
			Quote.Visible = true
			Next_Button.Visible = true
			Submit_Button.Visible = false
			TryAgain_Button.Visible = false
			Answer_Label.TextColor3 = Color3.fromRGB(91, 255, 66)
		else
			WrongSFX:Play()
			Answer_Label.Text = Answer_Input.Text.." ❌"
			Answer_Label.Visible = true
			Answer_Input.Visible = false
			Quote.Text = "“A person who makes few mistakes makes little progress.”"
			Quote.Visible = true
			TryAgain_Button.Visible = true
			Submit_Button.Visible = false
			Next_Button.Visible = false
			Answer_Label.TextColor3 = Color3.fromRGB(255, 49, 49)
		end	
	end)
	
	Next_Button.MouseButton1Click:Connect(function()
		ClickSFX:Play()
		Question.Visible = false
		game.Players.LocalPlayer.PlayerGui["Game UI"].SportsScreen["Question 2"].Visible = true
	end)

	TryAgain_Button.MouseButton1Click:Connect(function()
		ClickSFX:Play()
		Answer_Input.Text = ''
		Hint_Button.Visible = true
		Quote.Visible = false
		TryAgain_Button.Visible = false
		Submit_Button.Visible = true
		Answer_Label.Visible = false
		Answer_Input.Visible = true
	end)	
	
	local TT_TweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local TT_Tween = TweenService:Create(ToolTip, TT_TweenInfo, {BackgroundTransparency = 0.5})

	local TT_TweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local TT_Tween2 = TweenService:Create(ToolTip, TT_TweenInfo, {BackgroundTransparency = 1})
	
	Hint_Button.MouseEnter:Connect(function()
		ToolTip.Visible = true
		TT_Tween:Play()
	end)

	Hint_Button.MouseLeave:Connect(function()
		TT_Tween2:Play()
		wait(0.2)
		ToolTip.Visible = false
	end)
end

QuestionAnswer("4years", "4", "four", "fouryears", game.Players.LocalPlayer.PlayerGui["Game UI"].SportsScreen["Question 1"])

As you can see in the code, I’ve added 4 answer parameters to the function to support all the expected user answers.


Requirement 2

Let’s say there is the same question, “The Olympics are held every how many years?”

The user may accidentally add unwanted spaces, so to fix that we can just remove all white spaces in the answer and match if there are 4, “4years”, “fouryears” or “four”No Whitespace


Requirement 3

Let’s say there is the same question (again), “The Olympics are held every how many years?”

Expected Correct User Answers: “4 Years”, “4 YEARS”, “4 YeArS” + 100 more combinations.

The user may use different cases. So, we need are code to convert all the user input to lowercase letters. Then we can match if the user’s answer is equal to “4years”, “4”, “fouryears”, “four”Lowercase letters


Requirement 4

Let’s say there is the same question (once again), “The Olympics are held every how many years?”

THE BUG :beetle:

If a number contains 4 the computer will think the number is correct.

Image20220513113825

I don’t know how to fix this bug, this is where I need your suggestion to fix this bug!

1 Like

you could use string.find to see if “four” or “4” is in a string

I tried that but it’s even worse!

Let’s say the answer to a question is “Yes” if the user types “Yikes” the code will give that answer as correct because it contains all the letters in “Yes”. Compared to String.find, String.match is way better because it checks if all the words are correctly aligned together.

1 Like

why not hold a table for the answers?
for your olympics example, could you do:
local correctAnswers = {4, "4 years", "four", "four years"}
say they answer:
local answer = 4
then do something like

if tonumber(answer) then -- the answer is numerical (4 not "4")
    if table.find(correctAnswers, answer) then
        --it's a correct number
    else
        --it's an incorrect number
    end
else if table.find(correctAnswers, string.lower(answer)) then -- if non-numerical, find the string *set to all lower characters*
        --it's a correct string
    else
        --it's an incorrect string
    end
end

to see if they got it right

if you wanted to go further you could remove all non-alphanumericals from it so " four ._-` years " would properly compare to the answer keys’ “fouryears”

Can’t you use string.match using the ^ anchor to just match either the word “four” or the number “4” at the beginning of the word?

local s = 'four'
local matched1, matched2 = s:gsub('%s', ''):lower():match('^[%w]+') -- will match to "four", so
if matched1 == 'four' or matched1 == '4' then -- the matched string begins with either "four" or "4"
        
end

This isn’t related to your bug, but your examples aren’t accounting for this:
Queston: The Olympics are held every how many years?
Answer: The Olympics are held every four years.

The matter of fact is that the user can write anything into that field, expect it to be correct and be burned when a clearly correct answer is rejected.

In addition to improving your script’s language processing, consider shaping the user’s expectations.

Either teach the user that a well-formed answer is just the number 4 or the word “four” with no leading or trailing bits, or make the answer a form that the answer has to fit into, i.e. make the user fill in the statement “The Olympics are held every ____ years.

1 Like

I appreciate your feedback and will use it to improve my game! :grinning: :wrench: