Cancelling / Closing a thread inside the thread

I need to have a spawn function or a coroutine wrap in my script, for performance i want to close the threads. The problem is: when I try to close / cancel the thread i made from inside of the thread (the spawned function or coroutine wrap function) it errors and doesn’t cancel it.

Here is the way i am cancelling the thread:

local thread = nil

thread = coroutine.wrap(function()

	print("Hello World!")

	coroutine.close(thread)

end)()	

I also tried this with task.spawn and task.cancel yet it also gave me an error.

Any help will be appreciated!

1 Like

Why do you need to close the thread manually? It closes by itself after completing

3 Likes

Should’ve checked that! Thank you for answering.

1 Like

By the way I’d probably use the task library unless you have a specific use for coroutines. They’re quite different but if task.cancel and task.spawn work well together. Have a nice day :slight_smile:

1 Like

I knew about those, i was just wondering (in this example) how you could close the task (task.cancel) within the function of task.spawn.

Though it won’t be necessary as a thread closes when it has completed.
Thank you for the comment on it though!

1 Like

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