Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
Xiousa 156
5 years ago
Edited 5 years ago

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
0
Thank's a bunch! BlauEdward 15 — 5y
0
:wait() is deprecated. use :Wait() yHasteeD 1819 — 5y
0
When I use this method, it picks up on one click 1, 2, or even 4 times... BlauEdward 15 — 5y
Ad

Answer this question