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 1 year ago
Edited by ScriptGuider 1 year 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

spawn(function()
    while true do
        --script here
        wait(5)
    end
end)

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 1 year ago
Edited 1 year 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:

local waitInterval = 5 -- loop cycle wait time
local loopCount = 0 -- loop cycle count

task.spawn(function()
    while true do
        loopCount += 1 -- increase loop count by 1

        -- if loopCount reaches 5, speed up the loop cycle
        -- by 3 seconds
        if loopCount == 5 then
            waitInterval = 2
        end

        -- wait loop cycle interval
        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 -- loop cycle wait time
local loopIsActive = 0 -- a boolean value that determines if our loop is running

task.spawn(function()
    while true do
        loopCount += 1 -- increase loop count by 1

        -- if loopCount reaches 5, break the loop
        if loopCount == 5 then
            break -- stops the loop
        end

        -- wait loop cycle interval
        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 -- loop cycle wait time
local loopIsActive = true -- external variale for controlling loop flow

task.spawn(function()
    while true do
        -- only run the code below if 'loopIsActive' is true
        if loopIsActive then

            loopCount += 1 -- increase loop count by 1

            -- if loopCount reaches 5, speed up the loop cycle
            -- by 3 seconds
            if loopCount == 5 then
                waitInterval = 2
            end

            -- wait loop cycle interval
            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:

-- settings for loop one and loop two.
local loopOne = {
    running = true, -- "running" by default
    waitInterval = 5,
    counter = 0,
}

local loopTwo = {
    running = false, -- "paused" by default
    waitInterval = 5,
    counter = 0,
}

-- loop one
spawn(function()
    while true do
        if loopOne.running then
            loopOne.counter += 1

            -- pause this loop every 5 loop iterations, and
            -- resume the second loop
            if loopOne.counter%5 == 0 then
                loopOne.running = false
                loopTwo.running = true
            end

            print("running loop 1")
        end

        wait(loopOne.waitInterval)
    end
end)

-- loop two
spawn(function()
    while true do
        if loopTwo.running then
            loopTwo.counter += 1

            -- pause this loop every 5 loop iterations, and
            -- resume the first loop
            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).

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 — 1y
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 — 1y
0
Alternatively, you can use coroutine.close() on the task.spawn() thread. T3_MasterGamer 2189 — 1y
0
@T3_MasterGamer I think you mean task.close, coroutine.close is geared towards coroutines created directly through the coroutine library. ScriptGuider 5640 — 1y
View all comments (3 more)
0
Sorry, task.cancel not task.close ScriptGuider 5640 — 1y
0
Oh ok! T3_MasterGamer 2189 — 1y
0
I never knew task.cancel exists, so I used coroutine.close everytime. T3_MasterGamer 2189 — 1y
Ad

Answer this question