How do use use spawn()?

I would like to know how to use spawn() properly. Currently I know it allows you to run multiple loops at once and that’s about it. I would like to have an example of where it is used. I have tried looking on the developers hub but I was confused with the explanation it gave.

15 Likes

Spawn is used to run a function in a separate thread without yielding the current one. It will run simultaneously with the rest of the tasks, but won’t delay/affect the main thread.

spawn(function()
    --Do whatever here
end)
16 Likes

You can use spawn function to run code in serial. Read more here: Roblox Globals | Documentation - Roblox Creator Hub

1 Like

spawn(f) executes f in a new “thread”, without yielding the current thread. Think of a thread as a traffic lane of instructions that need to be executed. When we create a new thread/traffic lane, instructions/vehicles can flow through it without being affected by instructions/vehicles in other threads/traffic lanes. If thread 1 has a road block (for example a wait(x)), thread 2, 3, etc. can continue as normal:

I do not recommend using spawn, as it has a built-in wait() at the beginning. Instead, use the coroutine library:

-- create a new coroutine and resume it with 2 passed to n
coroutine.wrap(function(n)
    wait(n)
    print("executed 2nd")
end)(2)

print("executed 1st")

--> executed 1st
--> executed 2nd

Edit: Nowadays you should use task.spawn!

81 Likes

Thank you for you help, I will definitely be using coroutine instead.

spawn(f) for example can be useful for certain concepts such as ScriptBuilders, and is not entirely useless and outdated. For example, This ScriptBuilder requires spawn(f) to run code.

1 Like

If he helped out, make sure to mark his post as the answer!

Can’t the script builder use the coroutine library instead? I’m not completely familiar with the intricate details of Roblox’s thread scheduler.

Of course, it can be done, but in terms of ease, spawn(f) would be the best bet for these types of concepts.

coroutine.wrap(f)() does not wait() before executing f (unline spawn), though, so it is very much the faster option. It’s only a few more characters, and also allows arguments to be passed to the coroutine’s resume.

2 Likes

Thank you for all the help, I guess I need to read up on coroutine now as it seems to be more useful.

1 Like

You seem to have a valid point, but I meant ease for the users/players. I reckon that any beginner or even some mediocre programmers would know what coroutine even is, as it is really convenient to just write:

spawn(function()
-- code
end)
8 Likes

I fully agree with you here.

I know this is a old thread, but for anybody who just saw this post and doesn’t understand the traffick lane explanation here is a simple explanation.

task.spawn(function()
     task.wait(5)
     print('2')
end)

print('1') --this will print FIRST

As you can see, task.spawn goes through the function but DOESN’T wait to finish the function before printing the line of code underneath it

10 Likes

example is here
spawn(function()
–Code Here
end)

1 Like