How to have a wait() running while other functions run at the same time?

So I’ve been working at this for a few hours now, and I just can’t figure out how to get the script to do a wait of 300 seconds while running other functions. Below is the basic layout of my script

local function clean()
    --Code
end

local function two()
     --Code
end

local function one()
    --Code
    wait(25)
    two()
end

one()
wait(300)
clean()

Please Help. Thanks.

2 Likes

You can use coroutines and other methods from the task library to handle yields while also executing other tasks.

--//Execute function after X seconds
task.delay(300, clean)

--//Execute function that has yield statements, prevent it from affecting main thread of execution
local function someAsyncFunction()
task.wait(3)
print'yay'
end

coroutine.wrap(someAsyncFunction)()
print'i run immediately yessir'
7 Likes

Sorry. More specifically, my problem is that the wait(300) does not even begin. I think it is due to the one() function having a wait in it as well, though I honestly have no clue.

2 Likes

The script just stops from progressing downwards after one() is called, though one() still carries out its function correctly and still fires two() properly

2 Likes

You could just add return after two() so that the function stops.

local function one()
    --Code
    wait(25)
    two()
    return
end
2 Likes

You could wrap your one function call in a coroutine so it doesn’t interfere with your wait(300) yield, if that’s what you want.

coroutine.wrap(one)()
task.wait(300)
clean()
2 Likes

Why not just put the 3 functions into 1 function and then putting them in a coroutine?

2 Likes

break is used with loops, return is essentially the same thing but for functions.

2 Likes

Oops, I switched the two.

thirtycharacterlimit

3 Likes

Add “return” to the end of two() and it should work the way you want it to. If for what ever reason this does not fix it, add a “return” to the very end of one() as well.

1 Like

I already said that :]

thirtycharacterlimit

1 Like

Others have pointed this out but… You could use a coroutine to accomplish this.

local function clean()
    --Code
end

local function two()
     --Code
end

local function one()
    --Code
    wait(25)
    two()
end

local one_CR = coroutine.wrap(one) -- Doing this allows you to run `one_CR` multiple times

one_CR() -- one _ Co-Routine
wait(300)
clean()

I hope this helps :ok_hand:

1 Like