How can i detect if object is coroutine or task library with typeof()?

Question is in topic, i’m making table cleaner like maid for my game, and i want to determine when to use coroutine.cancel vs task.cancel, any ideas?

1 Like

it really doesn’t matter coroutine.create() returns a thread and task.spawn() returns a thread which is the same thing

local thread = task.spawn(function()
	while true do
		print("printing...")
		task.wait()
	end
end)

coroutine.close(thread)

--or
local thread = coroutine.create(function()
	while true do
		print("printing2...")
		task.wait()
	end
end)

coroutine.resume(thread)

task.cancel(thread)

and since task.spawn() can take a function or a thread as an argument we can even do this

local thread = coroutine.create(function()
	while true do
		print("printing2...")
		task.wait()
	end
end)

task.spawn(thread)

turns out
you can use coroutine.close to close threads but task.cancel only works for task library

task.cancel can work with for the coroutine library too

Summary
local thread = coroutine.create(function()
	while true do
		print("printing...")
		task.wait()
	end
end)

task.spawn(thread)

task.wait(1)

task.cancel(thread) -- will stop thread after this

print("End")
1 Like

nice to know, thx for help too

1 Like

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