Help with math quiz script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to make a random math script and it doesn’t work like it should.
  2. What is the issue? Include screenshots / videos if possible!
    its always marks incorrect .
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local question1 = script.Parent.TextLabel
local Box1 = script.Parent.Box1

local function randMath()
    local op = math.random(1, 4)
    local a = math.random(0, 14)
    local b = math.random(1, 14)
    if op == 1 then
        question1.Text = a .. " + " .. b .. " ="
        return a + b
    elseif op == 2 then
        question1.Text = a .. " - " .. b .. " ="
        return a - b
    elseif op == 3 then
        question1.Text = a .. " x " .. b .. " ="
        return a * b
    else
        question1.Text = a .. " % " .. b .. " ="
        return a / b
    end
end

local function AskQuestion()
    local answer = randMath()
    local guess = tonumber(Box1.Text)
    return guess == answer
end

local correct = AskQuestion()


Box1.FocusLost:Connect(function()
    if correct then
        script.Parent.ImageLabel.ImageColor3 = Color3.new(0, 1, 0)
    else
        script.Parent.ImageLabel.ImageColor3 = Color3.new(1, 0, 0)
    end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Your checking the answer as soon as you’ve set the question so Box1.Text will always be empty at that point - which is why it always says its wrong.
This code will fix your current code just to get it working:

local function AskQuestion()
	local answer = randMath()
	return answer
end

local correct = AskQuestion()


Box1.FocusLost:Connect(function()
	if tonumber(Box1.Text)==correct then
		question1.Parent.ImageLabel.ImageColor3 = Color3.new(0, 1, 0)
	else
		question1.Parent.ImageLabel.ImageColor3 = Color3.new(1, 0, 0)
	end
end)
2 Likes

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