Is it possible to run script while also waiting?

I have a script which fires a function while also needing to continue, the problem is that function holds a wait in it which is necessary for the script to work as intended. Is it possible to continue the rest of the script whilst it is waiting? currently I have another script which I’ve temporarily moved the wait function to but I feel like that may not be the best way.

1 Like

Call your function using spawn()

spawn(function_name)

1 Like

You can use coroutines coroutine | Roblox Creator Documentation

1 Like

How would I pass arguments through along with the function? I cannot seem to get it to let me pass an argument through as it tells me it needs 1 argument when it fires.

task.spawn(function_name)

Would be a better option, it is much more efficient than

spawn(function_name)

Benchmarks:
image

And task.spawn() can also pass arguments

task.spawn(function_name, arg1, arg2, arg3, etc)

Though coroutine would still be more efficient than task.spawn().

1 Like

Thank you very much kind sir/mam!

1 Like

Hey there just one more question for you real quick, how do I get a coroutine to restart from the beginning? if I let it end it just dies and isn’t usable. and if I yield and resume it just starts from the bottom of the script.

I’m not too sure about this as I haven’t used coroutines a whole lot, but you could try using a loop inside the coroutine so that it doesn’t die.

1 Like