How to store threads in a table?

I’m trying to store threads in a table, to be able to close them later.
The issue is that when I store them in a table, they are not threads, but functions, so when I use coroutine.close(function), it will error saying it’s a function.

I tried setting the thread (which is technically a function) to nil, but it would still run, probably because I only got rid of the reference to the thread(technically function) and not the actual thread(technically function), and I couldn’t really find anything anywhere else.

Here’s a visualization of what I’m trying to do:

Threads = {}


local thread = coroutine.wrap(SomeFunction())
thread()

table.insert(Threads, thread)


--Some time later when I need to stop the thread from running
coroutine.close(Threads[1]) --THIS ERRORS

Thanks

coroutine.wrap will return a function that, when called, runs whatever function that was passed to coroutine.wrap, asynchronously.

coroutine.create will return an actual thread, you can then use task.spawn or coroutine.resume on that thread to run it.

1 Like

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