So have a serversided function in which multiple threads get created. I’m adding all the threads to a table and returning the table so I can cancel all the running threads. It’s working with the first 3, but when i try to cancel the 4rd one its erroring.
So as you can see the task.cancel isnt inside the task.spawn so it shouldnt be that problem.
I found a solution by just checking if the crashed attribute is false in the while loop (“while task.wait(1) and map:GetAttribute(“Crashed”) == false do”) and its working, but I would be interested in the problem i showed and why its erroring…
I’m running into some confusion so I’m reviving this thread.
What is the point of task.cancel() if not to cancel an actively running thread? The documentation is extremely spotty and unclear: task | Documentation - Roblox Creator Hub. What does it mean by “the currently executing thread… may not be canceled”? Is that not the whole point of task.cancel(), to take in some thread that you have actively and currently running, and then kill it?
If for whatever reason it is unable to do its one job as stated in its name “cancel”, is it necessary to task.wait(5,runningthread) first, followed by task.cancel(runningthread)
If it is necessary to pause a thread before cancelling it the documentation should be updated to reflect this as this can be a major point of confusion.
The documentation is not clear about the conditions that make threads unable to be cancelled. My intuition says that if some particular thread is going to error when you cancel it, then pausing it and cancelling it still won’t work because the engine just absolutely will “not allow” that thread to be cancelled. I’m going to avoid using task.cancel altogether and just have the thread check some condition and break its own loop once the condition is true
I’m having this same problem. I read over the documentation MANY times and still am not understanding what they mean. From my experience, you can’t cancel the thread (coroutine) if it’s either “normal” or “dead”, and that’s what’s stated in the documentation, but what does “normal” mean? It’s not elaborated upon.
It seems that sometimes canceling threads work, and sometimes it doesn’t. I also apologize for the bump.
Actually yesterday i just thought about this post and a solution for it too…
I came to the result that coroutines might be the better choice for cancelling threads, I mean you can call coroutine.close(co) instead of task.cancel(f) and the documentation for this is more clear.
This part is actually pretty interesting… the documenation also talks about not beeing able to cancel some threads, for example when they’re running but i just did a quick test:
local co = coroutine.create(function()
while task.wait() do
print("running")
end
end)
coroutine.resume(co)
task.wait(1)
coroutine.close(co)
The prints stop after 1 second of waiting, even if the documentation says it shouldn’t be possible, right?
As a final result I can defenetly recommend using coroutine.close instead of task.closeall the time, as I can see no advantage in task.close. Roblox should depecate it.