How do you thread the code properly with spawn()?

If I do

spawn(func)

then the scripts stops at that point. And if I do

spawn(function() func end)

then the script skips spawn().

What am I doing wrong?

If you have any other suggestions how to run functions in the background without stopping the script, I’m open to hear it.

What do you mean it stops the script? Can you show us your exact code? Similarly, I don’t understand what you mean by the script skipping the spawn call. Please elaborate so we can help further.

In regard to running functions in the background, there are several methods:

delay(0, function ()
-- stuff
end)

spawn(function ()
-- stuff
end)

local co = coroutine.create(function ()
-- stuff
end)
co.resume()

coroutine.wrap(function ()
-- stuff
end)()

Exact code is pretty long, so I’m gonna send a short version.
By stop I mean, the script doesn’t go past that line while still running the function in spawn() (with a while true do loop).

function printing()
while true do print(1) wait(1) end
end

function printing2()
while true do print(2) wait(1) end
end

spawn(printing()) -- Script doesn't go past this line.
printing2() -- 

It’s because you’re calling the function instead of passing the function.

spawn(function ()
	while true do
		print(1)
		wait(1)
	end
end)

-- or

local function printing()
	while true do print(1) wait(1) end
end

spawn(printing)

Ah, it works without the () now. But I need to pass some parameters in my exact code…

If you want to pass parameters I recommend coroutines. e.g.

local myThreaddedFunction = coroutine.wrap(function (args)
	for i = 1, #args do
		print(i, "-->", args[i])
	end
end)

myThreaddedFunction({"Print", "Me", "Please"})

If not, you can do the same with a spawn function:

local function otherImportantFunction(args)
	table.foreach(args, print)
end

local function doSomething(a, b)
	spawn(function ()
		otherImportantFunction{a, b}
	end)
end

doSomething("Hello,", "world")
1 Like

You can’t do Lua spawn(func) because this will basically error as that’s not how you do Threads
As well When you do it like this Lua spawn(function() func end) The probem here is that you’re not giving in the right arguments So what you would to make a thread in your Script you can either user Courintines or the spawn function

** Spawn Function**
To start the new thread you will need to do

spawn(function() --In the here is where you can put your arguments
  -- Do your stuff in here
     print("New thread")
end)

Or
You can do a Courintine instead
And The difference between a Courintine and a Spawn function is that a Spawn function has a delay compared to a Courintine They both create new threads in a script
Example

local NewThread = coroutine.create(function () --In the here is where you can put your arguments
-- Do your stuff in here
print("New Thread created")
end)
NewThread.resume()

Also with coroutines you can resume and yield the at any given time
If you want to know more about Spawn Functions and coroutines you can go read this post that was made a while ago Here

Spawn code ain’t running for some reason, so I switched to coroutine.wrap, which works now.
Here’s the code if you like to examine what I did wrong.

spawn(function() -- Not running
while true do
for i=1,40 do joinBrick.Transparency = joinBrick.Transparency+(i*0.001) wait(0.01) end
for i=1,40 do joinBrick.Transparency = joinBrick.Transparency-(i*0.001) wait(0.01) end
end
end)
print("test") -- Runs

The joinBrick is defined beforehand.

I just tested that in Studio and it works?

local joinBrick = game.Workspace.Part
spawn(function() -- Not running
while true do
for i=1,40 do joinBrick.Transparency = joinBrick.Transparency+(i*0.001) wait(0.01) end
for i=1,40 do joinBrick.Transparency = joinBrick.Transparency-(i*0.001) wait(0.01) end
end
end)
print("test") -- Runs

Have you ensured you defined the variable ‘joinBrick’?

Weird. Maybe because spawn() has to run directly, like no waiting for events to happen?

function createJoinBrick()
	local a = Instance.new("Part", workspace)
	a.Shape = Enum.PartType.Ball
	a.Name = "joinBrick"
	a.BrickColor = BrickColor.new("Smoky grey")
	a.Anchored = true
	a.CanCollide = false
	a.Transparency = 0
	a.TopSurface = Enum.SurfaceType.Smooth
	a.Size = Vector3.new(10,10,10)
	a.Position = Vector3.new(0,0,0)
	return a
end

-- Following code is nested inside a 'if x do' which is nested inside Touched:Connect() event.
local joinBrick = createJoinBrick()
spawn(function() 
while true do
for i=1,40 do joinBrick.Transparency = joinBrick.Transparency+(i*0.001) wait(0.01) end
for i=1,40 do joinBrick.Transparency = joinBrick.Transparency-(i*0.001) wait(0.01) end
end
end)

Quickly scrapped a part of the code, which works.