Script doesn't work bruh -_-

i am making a roblox math game using only gui, the user gets asked a question with a text label and the player answers with a text box, but the issue here is that he gets asked a question if he gets it correct or false the correct text of text label will be “Correct” and wrong text “Incorrect, Try Again !”
but when either of them occurs they wait 10 seconds the text stays static it doesn’t give another math question, what’s wrong?
script:

local textLabel = script.Parent
local frame = textLabel.Parent
local textBox = frame:FindFirstChildOfClass("TextBox")

if not textBox then
    warn("TextBox not found in the same frame as the TextLabel")
    return
end

-- Function to generate a random math question
local function generateMathQuestion()
    local num1 = math.random(1, 10)
    local num2 = math.random(1, 10)
    local question = string.format("What is %d + %d?", num1, num2)
    local answer = num1 + num2
    return question, answer
end

-- Set the question in the TextLabel
local question, correctAnswer = generateMathQuestion()
textLabel.Text = question

-- Function to check the player's answer
local function checkAnswer()
    local playerAnswer = tonumber(textBox.Text)
    if playerAnswer == correctAnswer then
		textLabel.Text = "Correct!"
		wait(10)
		generateMathQuestion()
    else
		textLabel.Text = "Incorrect, try again."
		wait(10)
		generateMathQuestion()
    end
end

-- Connect the TextBox's FocusLost event to check the answer
textBox.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		checkAnswer()
		end

end)

Youre never updating the value, only once, update the text label in the checkanswer function

It is because you are only asking the question once.
Use a for loop to ask many questions.

for i = 1, 5 do --// asks the question 5 times
    -- ask the question
end

In your checkAnswer function you call generateMathQuestion() after wait(10).
However, you don’t update the TextLabel with the new question.
This generates a new question that doesn’t display on the text label:

local function checkAnswer()
    local playerAnswer = tonumber(textBox.Text)
    if playerAnswer == correctAnswer then
        textLabel.Text = "Correct!"
        wait(10)
        generateMathQuestion()  
    else
        textLabel.Text = "Incorrect, try again."
        wait(10)
        generateMathQuestion() 
    end
end

Instead, try this:

local function setNewQuestion()
    local question, correctAnswer = generateMathQuestion()
    textLabel.Text = question
    return correctAnswer
end
local function checkAnswer()
    local playerAnswer = tonumber(textBox.Text)
    if playerAnswer == correctAnswer then
        textLabel.Text = "Correct!"
    else
        textLabel.Text = "Incorrect, try again."
    end
    wait(10)
    correctAnswer = setNewQuestion() 
end

Since both if statements still apply to

wait(10)
		generateMathQuestion()

We can move it to the end of the function.

Additionally, we can add

textBox.Text = " "

To clear the textbox to prepare for the next answer.

uhhhhhhh didn’t understand wanna make full script plz

1 Like

no i want always
111111111111111111111111111

Here’s the full version:

local textLabel = script.Parent
local frame = textLabel.Parent
local textBox = frame:FindFirstChildOfClass("TextBox")

if not textBox then
    warn("TextBox not found in the same frame as the TextLabel")
    return
end

-- Function to generate a random math question
local function generateMathQuestion()
    local num1 = math.random(1, 10)
    local num2 = math.random(1, 10)
    local question = string.format("What is %d + %d?", num1, num2)
    local answer = num1 + num2
    return question, answer
end

-- Function to set a new question in the TextLabel
local function setNewQuestion()
    local question, correctAnswer = generateMathQuestion()
    textLabel.Text = question
    return correctAnswer
end

-- Initialize with the first question
local correctAnswer = setNewQuestion()

-- Function to check the player's answer
local function checkAnswer()
    local playerAnswer = tonumber(textBox.Text)
    if playerAnswer == correctAnswer then
        textLabel.Text = "Correct!"
    else
        textLabel.Text = "Incorrect, try again."
    end
    wait(10)
    correctAnswer = setNewQuestion()
    textBox.Text = ""
end

-- Connect the TextBox's FocusLost event to check the answer
textBox.FocusLost:Connect(function(enterPressed)
    if enterPressed then
        checkAnswer()
    end
end)

I hope you understand with the insight I provided!

1 Like

is it possible to make the text uneditable after correct answer ?
but if it is wrong answer the text will be editable please :slight_smile: and yes it works !!
and make it if the answer is incorrect the asked question shows up again after 3 seconds @S7N0W

Btw checked ur code it looked good, compared it with ai’s, so much more compiled and organized nice job!

1 Like

What? i didn’t use ai or did i understand worng? sorry if it’s a misunderstanding

No i was saying that i compared @S7N0W code with a version of code i got from Ai and @S7N0W ‘s code was alot more compiled and neat.
It made more sense, but that script should work for you

ok thank
1111111111111111111111111