Is there any way to run multiple functions at the same time?

As the title suggests, is there a way to run multiple functions at the same time, in the same script? Or do I need to use multiple scripts?
There are a few other posts with a similar topic but they don’t really help me that much
Basically what im trying to do is:

local function Function1()
    --do stuff
end
local function Function2()
    --do more stuff
end
Function1()
Function2()

Its only running one function after the other finishes. Any explanation would be appreciated since Im still new to coding

task.spawn(Function1)
task.spawn(Function2)

This will call them asynchronously i.e in a separate thread, and you can pass args to the functions as the next parameters.

2 Likes

What if there were parameters to the function?

2 Likes

I use coroutine.wrap(TheFunction)() which allows to run functions without Yielding. If you have parameters you add them like coroutine.wrap(TheFunction)(First, Second) --Etc.

Your more then welcome to learn more with this roblox documentation

4 Likes
task.spawn(Function1, parametersToFunction1)
task.spawn(Function2, parametersToFunction2)
8 Likes

A coroutine doesn’t truly run in parallel. It gives the illusion of doing multiple things at once, by quickly switching between the functions.

If you want them to truly run in parallel, you have to use parallel lua. What’s your planned usecase?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.