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.
= Accomplished
= Uncompleted
- It should accept multiple answers.
- It should not be whitespace sensitive.
- It should not be case-sensitive.
- It should not get confused with numbers.
Let’s see these requirements in action!
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