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:
local waitInterval = 5
local loopCount = 0
task.spawn(function()
while true do
loopCount += 1
if loopCount == 5 then
waitInterval = 2
end
task.wait(waitInterval)
end
end)
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:
local waitInterval = 5
local loopIsActive = 0
task.spawn(function()
while true do
loopCount += 1
if loopCount == 5 then
break
end
task.wait(waitInterval)
end
end)
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:
local waitInterval = 5
local loopIsActive = true
task.spawn(function()
while true do
if loopIsActive then
loopCount += 1
if loopCount == 5 then
waitInterval = 2
end
task.wait(waitInterval)
end
end
end)
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:
local loopOne = {
running = true,
waitInterval = 5,
counter = 0,
}
local loopTwo = {
running = false,
waitInterval = 5,
counter = 0,
}
spawn(function()
while true do
if loopOne.running then
loopOne.counter += 1
if loopOne.counter%5 == 0 then
loopOne.running = false
loopTwo.running = true
end
print("running loop 1")
end
wait(loopOne.waitInterval)
end
end)
spawn(function()
while true do
if loopTwo.running then
loopTwo.counter += 1
if loopTwo.counter%5 == 0 then
loopOne.running = true
loopTwo.running = false
end
print("running loop 2")
end
wait(loopTwo.waitInterval)
end
end)
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).
7 comments
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.
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.
Alternatively, you can use coroutine.close() on the task.spawn() thread.
@T3_MasterGamer I think you mean task.close, coroutine.close is geared towards coroutines created directly through the coroutine library.
Sorry, task.cancel not task.close
Oh ok!
I never knew task.cancel exists, so I used coroutine.close everytime.