Math problem is "wrong" but my answer is correct

So I made a math question GUI and if you get it right it prints correct and if you get it wrong it prints wrong. I answer the questions correctly but the game is telling me it’s wrong.

local Player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")

local QuestionText = script.Parent.QuestionUI.Question

local function newQuestion()

	local firstArgument = math.floor(math.random(1, 10))
	local secondArgument = math.floor(math.random(1, 10))
	
	QuestionText.Text =  firstArgument.." ".."+".." "..secondArgument.." ".."="
	
	return firstArgument + secondArgument
	
end


local function startCountdown()
	
	local answer = newQuestion()
	print(answer)
	
	local guiInfo = TweenInfo.new(
		10,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.In,
		0,
		false
	)
	
	local props = {
		Size = UDim2.new(0, 5,0, 6);
	}
	
	local tween = tweenService:Create(script.Parent.QuestionUI.Countdown, guiInfo, props)
	tween:Play()
	tween.Completed:Wait()
	
	local Answer = script.Parent.QuestionUI.Answer.Text:gsub("%s","")
	
	if #Answer == 0 then
		
		print("no answer")
		
	elseif #Answer > 0 then
		if Answer == answer then
			print(Answer)
			print("correct")
		else
			print(Answer)
			print("wrong")
		end
		
		
	end
end

task.wait(2)
script.Parent.QuestionUI:TweenPosition(UDim2.new(0.362, 0,0.688, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, 0.5, true)
task.wait(0.5)

startCountdown()

It seems like you’re comparing a string with a number value

this is never gonna be equal, only if you convert the string to a number or viceversa, with tonumber(string) and tostring(number).

ex:

if tonumber(Answer) == 0 or Answer == tostring(0) then
1 Like

hey i got this error, Players.RangelBrothers.PlayerGui.MathQuestion.Settings:45: attempt to compare number < nil

	local Answer = script.Parent.QuestionUI.Answer.Text
	
	if tonumber(Answer) == 0 then		
		print("empty")	
	elseif tonumber(Answer) > 0 then ---  LINE WHERE THE ERROR IS 
		if tonumber(Answer) == answer then
			-- correct
		else
			-- wrong
		end	
	end

That means the answer is nil in that line, meaning there isnt a value to compare to > 0.

In other words,

tonumber(nil) -> nil

if nil > 0 then --[[ERR]]
1 Like

How would I be able to solve the issue?

Wait nevermind, I found a solution, thanks for your help!

1 Like

Somethings wrong with how or when you’re getting the text.

It means when you define the “Answer” there isnt any value in it.

this is equal to nil

1 Like

Not necessarily set to ‘nil’, just set to a string literal that cannot be coerced into a number literal via tonumber.

tonumber("Hello world!") --nil
tonumber("") --nil
2 Likes