Having troubles with Coroutines

Hello. I’ve been trying to learn coroutine but I’ve seem to be in a pickle. I have the following code as an example.

local StartTimer = coroutine.wrap(function()
	for i = 1,10,1 do
		wait(1)
		print("Starting in "..10-i)
	end
	
	print('Start!')
	
	coroutine.yield()
end)

while true do wait(20)
	StartTimer()
	
	print('Started Timer')
end

When I do StartTimer() twice, it doesn’t seem to fire on the second time for some reason. Could someone help please? I don’t know if I’m just being dumb.

you already made the coroutine now that it’s yielded you need to call coroutine.resume(coroutine) otherwise it won’t do anything

Oh thanks, sorry I didn’t know yield was the problem.

Does this work efficiently?

local StartTimer = coroutine.create(function()
	while true do
		for i = 1,10,1 do
			wait(1)
			print("Starting in "..10-i)
		end

		print('Start!')

		coroutine.yield()
	end
end)

while true do wait(20)
	coroutine.resume(StartTimer)

	print('Started Timer')
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.