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

Is there a way to break a while loop outside it?

Asked by 8 years ago

I want to make a while loop that breaks when a function is called,but i don't know how to do it! Here is the while loop of my script and the function that breaks the loop

01while true do
02    script.Parent.TweenableValue.Tween:Invoke(thing,'InOut','Quad',20,true)
03    repeat
04        wait()
05    until value.Value==thing
06    script.Parent.TweenableValue.Tween:Invoke(-thing,'InOut','Quad',20,true)
07    repeat
08        wait()
09    until value.Value==-thing
10    print'done'
11end
12 
13function play()
14    print'play'
15    killplayer:FireServer()
View all 22 lines...
0
Have a variable set to true, and instead of while true do, use while VarName do. Then set the bool to false in the function? iamnoamesa 674 — 8y
0
^ Yeah, that works AstrealDev 728 — 8y

1 answer

Log in to vote
3
Answered by 8 years ago
Edited 7 years ago

Yes, but not in the same way you're trying to do. Loops won't execute the next line of code until whatever condition they're given is met. Therefore, using an infinite while true statement will run the loop forever, never executing the code after the loop's block, meaning your function play was never evaluated.

Synopsis


You typically don't want while loops running in different places (for this to even be possible, coroutines would be involved, which is a whole other story). Whatever it is you're trying to do, there's probably a much better solution for it.

Answer


To answer your question, though, you would want the condition of your while loop to be mutable (not constant). That way, you can change the variable the while loop is using as a condition, to terminate the loop next iteration. Example:

01local running = true -- Create condition
02 
03-- This is what I was talking about before, with the involvement of coroutines. While this is a solution, I highly don't recommend it for anything other than practice.
04spawn(function()
05    while running do
06        print("Loop is running")
07        wait()
08    end
09    print("Stopped running")
10end)
11 
12-- Some function that stops the loop outside of itself by setting 'running' to false
13local function stopRunning()
14    running = false
15end
16 
17-- Wait 1 second before stopping the loop
18wait(1)
19stopRunning()

The code above is just for practice, I don't recommend using it in any project you're working on. However, this would be the answer to your question. If need help understanding anything, just let me know.

Edit:

Adding a "pause and play" mechanic

Creating a "pause and play" mechanic to a loop, would implement coroutines. You don't have to use them directly, but they're being used one way or another (with other functions like spawn), so I'm going include them in my example.

01-- ~ ScriptGuider
02-- Library functions
03local create = coroutine.create
04local resume = coroutine.resume
05local yield = coroutine.yield
06local status = coroutine.status
07local floor = math.floor
08 
09-- A pseudo instance to provide interface for a loop
10local loop = {}
11 
12-- Local pause function, to be used within the loop's code.
13local function Pause()
14    print("Loop paused from internal coroutine")
15    loop.Running = false
View all 65 lines...

Looks a bit complicated, as it should for a situation like this. This is probably the closest I could get to giving an example without making an entire task-scheduler. This basically just utilizes the yield function of the coroutine library, which exits the code block's running state, and can be used to queue it up to later be resumed. During this intermediate state, the program can spend time handling other tasks so no time is wasted.

0
I got it. Thank you for helping me. I was going to ask another thing about the tween in the loop,but i see i can stop it. Here is your accept :D arthurgps2 67 — 8y
0
Also,with this while loop example,you gave me another question (3,actually). Is the code above going to be executed when the condiction is false in the loop? If it does,is the while loop going to execute again if the condiction is true? And the above code? arthurgps2 67 — 8y
0
That's something I thought you might ask, but left it out to keep things short. It's pretty easy, you'd just spawn a new function when you call play. Again, wouldn't recommend it, but I'll edit the answer and include it. ScriptGuider 5640 — 8y
Ad

Answer this question