Please help with coroutine!

Try without directly calling it maybe it’s a lua bug. Do it just like mine.

Pretty sure it wasn’t a typo, i’ll send the whole code in a sec

This is your code, word-for word:

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()

These are the lines that error:

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

Fix the lines by removing the ():

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

The script will now work exactly as intended.
The error tells you exactly how it is:

08:23:05.650 Workspace.Part.Script:16: missing argument #1 to ‘wrap’ (function expected) - Server - Script:16

It means that you were previously doing this:
coroutine.wrap(nil)
Because of this:

print(co1()) -- prints nil
print(co1() == nil) -- prints true
print(co1) -- prints function

Look at the error again:

08:23:05.650 Workspace.Part.Script:16: missing argument #1 to ‘wrap’ (function expected) - Server - Script:16

function expected

nil is not a function. co1() is not a function, it is nil. co1 is a function. co1 returns nil.

1 Like

Sorry for the super late reply, but, how do you pass arguments through the coroutine?

By calling it like a function?

local function printLater(str)
	print("Waited " .. wait(0.5) .. " seconds")
	print(str)
end

local wrapped = coroutine.wrap(printLater)
wrapped("asdf")

OK, that’s coroutine.wrap, which returns a function that resumes a coroutine, not an actual coroutine/thread.
To “pass arguments” into a coroutine, you just add them as arguments to coroutine.resume.
This is what coroutine.wrap does for you (except it only creates the coroutine once, resuming the same one every time later on):

local coro = coroutine.create(printLater)
coroutine.resume(printLater, "asdf")

It’s not relevant to your question, but everyone who wants to learn about coroutines should know that wait() and similar also work by yielding the current thread and having it resumed by something else later.

local function printLater(str)
	print("Waited " .. wait(0.5) .. " seconds")
	print("Testing " .. coroutine.yield())
	print(str)
end

local coro = coroutine.create(printLater)
coroutine.resume(printLater, "print(str)") -- str = "print(str)"
coroutine.resume(printLater, "uh-oh!!!") -- print("Waited uh-oh!!! seconds")
-- after 0.5 seconds
-- print("Testing 0.5")
-- print("print(str)")