Please help with coroutine!

I’ll just paste the code and the output following it, there’s not much more context to add here (no pun intended, lol).

local function co1()
	for i = 60, 0, -1 do
		wait(0.5)
		print("co1")
	end
end

local function co2()
	for i = 60, 0, -1 do
		wait(1)
		print("co2")
	end
end
coroutine.wrap(co1())
coroutine.wrap(co2())
co1()
co2()

08:22:29.845 Baseplate auto-recovery file was created - Studio - C:/Users/boii/Documents/ROBLOX/AutoSaves
08:22:32.382 co1 - Server - Script:6
08:22:33.612 Go to Game Settings and turn on Allow HTTP requests. - Studio - Game Settings
08:22:34.617 :arrow_forward: co1 (x60) - Server - Script:6
08:23:05.650 Workspace.Part.Script:16: missing argument #1 to ‘wrap’ (function expected) - Server - Script:16
08:23:05.651 Stack Begin - Studio
08:23:05.651 Script ‘Workspace.Part.Script’, Line 16 - Studio - Script:16
08:23:05.651 Stack End - Studio

1 Like
coroutine.wrap(function()
	for i = 60, 0, -1 do
		wait(0.5)
		print("co1")
	end
end)()
coroutine.wrap(function()
	for i = 60, 0, -1 do
		wait(0.5)
		print("co2")
	end
end)()

Do you have an explanation of why that works?

Beginners Guide to Coroutines (roblox.com)

1 Like
local co1 = coroutine.wrap(function()
	for i = 60, 0, -1 do
		wait(0.5)
		print("co1")
	end
end)
local co2 = coroutine.wrap(function()
	for i = 60, 0, -1 do
		wait(0.5)
		print("co2")
	end
end)
co1()
co2()
2 Likes

What this does is call co1() and co2() before using their results (which are none) as arguments to coroutine.wrap. What you wanted was

coroutine.wrap(co1)()
coroutine.wrap(co2)()

Since coroutine.wrap returns a “coroutine’d” version of the function you pass.

3 Likes

Got it, although I think they could’ve went another route with it (after you do the coroutine.wrap() the function itself becomes a thread), I still have one more question: in the code you sent to me you are not assigning a variable, how does that work?

cause instead of assigning it to a variable it just calls it on the spot with the original reference

1 Like

Thanks, and the “coroutine’d” would be called a thread.

For the most part in Lua it’s the same thing, there is a reason the library is called coroutine ;p

image

Let’s not be pedantic here, have a look at the library name. Feel free to DM me if you wish to continue.

2 Likes

I’d just like to add, if anyone more is reading this and was confused too, “Think of coroutine.wrap() as a function” would mean that just like a function you have to call the coroutine.wrap() and coroutine.wrap(func) would be defining the function, and then just like a function you have to assign the specific coroutine.wrap() to something to call it later i.e. a variable.

However when I did this, it didn’t work, so yeah confusion everywhere lol, although I’d like to know why it doesn’t work maybe it would be a bit too much.

image

Looks like you are trying to wrap the returned value of the co1 function which probably isnt a function

Remove the () from co1 and co2. Functions are first-class values – you can pass them as arguments.

I tried it before it just gave me an error.

Can’t repro.

image

That code is not the same as mine.

You said it didn’t work – however it works fine for me. Likely a mistake (such as a typo) on your end