What is spawn(function) end) used for?

The title says it all, so basically I want to know what ‘spawn(function()’ is used for.

I have seen it used before, but I don’t know what it is used for, or even understand it.

7 Likes

Spawn is used to execute code on other threads. Coroutines do the same but with way more features. Read this excellent tutorial on the Roblox wiki:

https://developer.roblox.com/articles/Beginners-Guide-to-Coroutines

13 Likes

https://developer.roblox.com/api-reference/lua-docs/Roblox-Globals#Spawn

2 Likes

Sorry for asking a simple question. Turns out it has something to do with threads in a computer processor(Such as AMD or Intel). I should have spent more time searching scripting helpers, or the developer hub.

2 Likes

It’s completely fine. We all need to learn. Maybe next time, research your topic a bit more extensively before you post a question; that will save your and our time (although we don’t mind helping people out; that is why we’re here).

Have a great day/evening/night.

3 Likes

Actually it doesn’t. Lua executes parts of code in series on a single CPU thread. In other words, only one part of the script can be executed at any given moment, while the others wait.

6 Likes

spawn() is probably my favourite thing ever in Roblox scripting. It helps your script to do multiple things at the same time, without the need to write separate scripts. Consider following code:

local parts = {}
local sizeVector = Vector3.new(1,1,6)
for i = 0, 1000 do
 parts[i] = Instance.new("Part")
 parts[i].Size =  sizeVector
 parts[i].CFrame = CFrame.new(i,i,0)
 parts[i].Anchored = true
 parts[i].Parent = workspace
 spawn(function()
  wait(5)
  parts[i]:Destroy()
 end)
 wait(0.5)
end

This script will be making very long stairs, destroying each step after 5 seconds. Also lua will remember index value at the time of calling spawn, so no need to make copy of “i”. Really useful.

15 Likes

Going to be a little critic here: The code you provided has an unneeded wait() inside the function you’re spawning. More of, because spawn() has a built in wait() in it before the code contained within it executes. And in your case, you’ll always end up waiting more then 5 seconds (upwards of 0.6 seconds longer!) before the ‘stair’ has :Destroy() called upon it!

This is where using delay() can come into handy. delay() functions similarly to spawn. I.E it spawns a new thread. But it has no wait() built within it. Unless you’re using delay(0, function) for some reason. This’ll allow you to begin new threads at a set time with little margin of error. Which can be more reliable in a lot of cases.

4 Likes

Thanks, that may come in handy. And we all probably should use coroutines anyway, but so far I have found no real use for yielding and spawn has been working wonders for me.

2 Likes

Or you could use coroutine.wrap(function() end) to avoid either of the two.

2 Likes

Or more performantly.

local Thread = coroutine.create(function(a)
	print("idk", a)
end)

coroutine.resume(Thread, "hi")
3 Likes

It’s not more performant. It’s actually a bit slower since you call a function twice.

1 Like

You call two functions too if you use your solution:

accounts for one, but

coroutineWrappedFunction();

will still call coroutine.resume.


Also, just because something has less function calls, doesn’t always mean it’s more performant.
Finally, Lily is a master of optimization too, so I trust her judgement.

1 Like

image

It actually is faster.

local Functions = { }
local random = math.random math.randomseed(tick() % 1 * 1E7)

local function float(lower, greater)
	return lower + random()  * (greater - lower)
end

local Thread = coroutine.create(float)

Functions["coroutine.wrap"] = function()
	coroutine.wrap(float)(-1E4, 1E4)
end

Functions["coroutine.resume"] = function()
	coroutine.resume(Thread, -1E4, 1E4)
end

require(2110831719).new(1, "Title", Functions)

Even if you create it inside of the function, it’s still way faster.

image

In other words, this performance difference is the same as 24 FPS and 60 FPS if it was a game benchmark. It’s a significant change.

7 Likes

Yeah, but your benchmark has a huge mistake. You need to create a thread before resuming it. I was talking about avoiding spawn while using the default Lua functions. coroutine.wrap is faster if you need to run the function once.

7 Likes