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

Is there a way to stop a looped script so the time it takes to loop can be changed?

Asked by 2 years ago
Edited by ScriptGuider 2 years ago

I'm pretty new to scripting and I'm working on a game with my friend just for fun and I've made a script to loop

1spawn(function()
2    while true do
3        --script here
4        wait(5)
5    end
6end)

Is there a script to make the loop stop or anything to add to the script to make it stop so I can change the amount of time, because when I use another script to loop the same script but with a different amount of time, that one stays and it loops every 5 seconds along with every 10 or however much seconds.

1 answer

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

Typically you don't want to run infinite loops in your code, unless you have one infinite loop that acts as the root of everything in your game (known as a game loop).

Side note: I would also recommend using task.wait and task.spawn over regular wait and spawn. The reason being, the task library is more closely aligned with ROBLOX's thread scheduler. You can read more about the task library here.

Variables

To answer your question, you can use a variable to represent your loop's wait interval, then change that variable whenever you want to change the interval amount. Here's an example:

Ex:

01local waitInterval = 5 -- loop cycle wait time
02local loopCount = 0 -- loop cycle count
03 
04task.spawn(function()
05    while true do
06        loopCount += 1 -- increase loop count by 1
07 
08        -- if loopCount reaches 5, speed up the loop cycle
09        -- by 3 seconds
10        if loopCount == 5 then
11            waitInterval = 2
12        end
13 
14        -- wait loop cycle interval
15        task.wait(waitInterval)
16    end
17end)

In this example, we're keeping track of how many loop cycles have occurred with our loopCount variable, and representing the loop cycle interval with the waitInterval variable.

Just to demonstrate how this could be useful, after the loopCount reaches a value of 5 we change the waitInterval value to 3 which will speed up the loop.

Breaking Loops

If you want to stop a loop completely, you can use the break or return keywords. If you only care about stopping the loop and nothing else, you should just use break. I recommend looking more into the difference between break and return on your own time.

Here's an example similar to the last of how we can stop a loop using break:

01local waitInterval = 5 -- loop cycle wait time
02local loopIsActive = 0 -- a boolean value that determines if our loop is running
03 
04task.spawn(function()
05    while true do
06        loopCount += 1 -- increase loop count by 1
07 
08        -- if loopCount reaches 5, break the loop
09        if loopCount == 5 then
10            break -- stops the loop
11        end
12 
13        -- wait loop cycle interval
14        task.wait(waitInterval)
15    end
16end)

In this new example, the loop will continue to cycle until the loopCount reaches 5. Then the loop stops immediately.

Caution

Just as a caution, using task.spawn should always be a last resort solution. 99.9% of the time you don't need to do multiple things at once. Usually, you can just run your code synchronously, or there is probably some event that exists that will do the job for you.

Definitely ask around if you find yourself using task.spawn or any thread-creating function for the purpose of simultaneity. Someone should be able to give you a better solution.

I hope this answer helped you out, let me know if you have any questions!

Edit:

There is no primitive way to toggle a loop without using the coroutine library to yield and resume threads. Each loop you spawn will continue to run in the background until it is broken.

This is one of the reasons why I said "be cautious when using spawn", because this can hog computer resources very quickly.

If Statements & "Toggling"

Regardless, you just need to create an if-statement with some external variable that controls the flow of the code inside the loop.

Ex:

01local waitInterval = 5 -- loop cycle wait time
02local loopIsActive = true -- external variale for controlling loop flow
03 
04task.spawn(function()
05    while true do
06        -- only run the code below if 'loopIsActive' is true
07        if loopIsActive then
08 
09            loopCount += 1 -- increase loop count by 1
10 
11            -- if loopCount reaches 5, speed up the loop cycle
12            -- by 3 seconds
13            if loopCount == 5 then
14                waitInterval = 2
15            end
View all 22 lines...

Now, whenever you change the loopIsActive variable to false, the code inside the loop won't execute. When you change it back to true, it will start executing again.

To "toggle" between loops, you would just do the same thing for your other spawned loop. Just keep in mind that this is a sub-optimal solution because using task.spawn here is a sub-optimal strategy. If you want a better solution, you should mention the context of the problem you're trying to solve and then we can start providing you with a better strategy.

Ex:

01-- settings for loop one and loop two.
02local loopOne = {
03    running = true, -- "running" by default
04    waitInterval = 5,
05    counter = 0,
06}
07 
08local loopTwo = {
09    running = false, -- "paused" by default
10    waitInterval = 5,
11    counter = 0,
12}
13 
14-- loop one
15spawn(function()
View all 52 lines...

A better solution would be to use coroutines for their actual pause/play mechanic, but that's a whole other topic entirely (and not necessary most of the time).

0
Thanks but I'm looking more for a script that is like a toggle, when the loop script I have is ran, it will loop and I'm looking for another script, that when ran, will stop that script so I can run the next loop script with a different set time instead of a singular script with set amount of loops. xxlolsdudexx 5 — 2y
0
I see. You can still employ the examples given here, as that wouldn't differ much from the first example. I'll update the answer with a third case. ScriptGuider 5640 — 2y
0
Alternatively, you can use coroutine.close() on the task.spawn() thread. T3_MasterGamer 2189 — 2y
0
@T3_MasterGamer I think you mean task.close, coroutine.close is geared towards coroutines created directly through the coroutine library. ScriptGuider 5640 — 2y
View all comments (3 more)
0
Sorry, task.cancel not task.close ScriptGuider 5640 — 2y
0
Oh ok! T3_MasterGamer 2189 — 2y
0
I never knew task.cancel exists, so I used coroutine.close everytime. T3_MasterGamer 2189 — 2y
Ad

Answer this question