Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I pause a for loop until a condition is met?

Hey! I was thinking about making a "multiple choice test" on ROBLOX. I used a for loop to get the questions from a ModuleScript. However, during the for loop, it blows through the questions. I was hoping that there was a way to pause the script until a Player click on any of the answers.

What I have right now:

function beginMultipleChoice(player)
	for i=1, #choiceInfo do
		MultipleChoice.Question.Text = choiceInfo[i].Question
		MultipleChoice.A.Text = choiceInfo[i].A
		MultipleChoice.B.Text = choiceInfo[i].B
		MultipleChoice.C.Text = choiceInfo[i].C
		MultipleChoice.D.Text = choiceInfo[i].D
		MultipleChoice.Visible = true
		MultipleChoice:TweenPosition(UDim2.new(0,0,0,0), "In", "Sine", 1)
		repeat until MultipleChoice.A.MouseButton1Click:wait(.1) do
			print("Waited")
		end
	end
end

When I click on "A" it does nothing.

I am in need of help and would appreciate it if anyone would help.

Thanks in advance!

1 answer

You've got the right idea, you just executed it wrong.

Event:wait() isn't used like a normal wait(). It yields until that event fires. Try using this:

function beginMultipleChoice(player)
    for i=1, #choiceInfo do
        MultipleChoice.Question.Text = choiceInfo[i].Question
        MultipleChoice.A.Text = choiceInfo[i].A
        MultipleChoice.B.Text = choiceInfo[i].B
        MultipleChoice.C.Text = choiceInfo[i].C
        MultipleChoice.D.Text = choiceInfo[i].D
        MultipleChoice.Visible = true
        MultipleChoice:TweenPosition(UDim2.new(0,0,0,0), "In", "Sine", 1)
        MultipleChoice.A.MouseButton1Click:wait()
        print("Waited")
    end
end

edit: You can use Coroutines to have it work with every answer:

function beginMultipleChoice(player)
    for i=1, #choiceInfo do
        local Answered = false
        MultipleChoice.Question.Text = choiceInfo[i].Question
        MultipleChoice.A.Text = choiceInfo[i].A
        MultipleChoice.B.Text = choiceInfo[i].B
        MultipleChoice.C.Text = choiceInfo[i].C
        MultipleChoice.D.Text = choiceInfo[i].D
        MultipleChoice.Visible = true
        MultipleChoice:TweenPosition(UDim2.new(0,0,0,0), "In", "Sine", 1)
        coroutine.resume(coroutine.create(function()
            MultipleChoice.A.MouseButton1Click:wait()
            print("WaitedForA")
            Answered = true
        end))
        coroutine.resume(coroutine.create(function()
            MultipleChoice.B.MouseButton1Click:wait()
            print("WaitedForB")
            Answered = true
        end))
        coroutine.resume(coroutine.create(function()
            MultipleChoice.C.MouseButton1Click:wait()
            print("WaitedForC")
            Answered = true
        end))
        coroutine.resume(coroutine.create(function()
            MultipleChoice.D.MouseButton1Click:wait()
            print("WaitedForD")
            Answered = true
        end))
        repeat wait() until Answered
    end
end

3 comments

0

Thank's a bunch!

0

:wait() is deprecated. use :Wait()

0

When I use this method, it picks up on one click 1, 2, or even 4 times...

This discussion is preserved as it was. New answers, comments, and votes are closed.