Task.Cancel() Errors(Basically says that it cannot cancel the thread)

I made a dialogue system that works fine. Although, there is an error that occurs when the player responds to the second option when the conversation tries to end.

Error: 3 Workspace.Map.Rig.DialogueScript1:43: cannot cancel thread

dialogueObject.responded:Connect((function(dialogueNum, responseNum)
	if currentThread then 
		task.cancel(currentThread)
	end
	currentThread = task.spawn(function()
		if dialogueNum == 1 then
			if responseNum == 2 then -- This causes the cancel errors
				dialogueObject:npcReply("You are a idiot, hahahahahaha!!!", true)
				dialogueObject:playerReply("You are more of a idiot", true)
				dialogueObject:exit()
			elseif responseNum == 1 then
				dialogueObject:exit("Ok Bye bozo!")
			end
		end
	end)
	
end))


dialogueObject.exitRequest:Connect(function()
	if not currentThread then return end
-- This is where the error occurs: 
-- Workspace.Map.Rig.DialogueScript1:43: cannot cancel thread 
	task.cancel(currentThread)
end)

Because thread is already dead, it just doesnt know it yet :santa_claus::santa_claus::santa_claus:

Do check like if coroutine.status(currentThread)~="dead" then task.cancel(currentThread) end

It still prints the same thing..

Can you do typeof() to check to see if currentThread is actually an thread?

Also when you cancel it, what state is the thread in exactly?

make sure wherever you cancel it you set it to nil too

It is possible that dialogueObject:exit() is firing dialogueObject.exitRequest immediately, which would mean that the thread is trying to cancel itself, which is not possible.

dialogueObject.exitRequest:Connect(function()
	if not currentThread then return end
	if currentThread == task.current() then
		return
	end
	task.cancel(currentThread)
	currentThread = nil
end)

Also add currentThread = nil, for the case where task.spawn ends naturally.

	currentThread = task.spawn(function()
		...
		currentThread = nil
	end)

=coroutine.yield() before canceling, also u can check if the thread is dead, and it will work. I don’t want to get into the details of coroutines, as it’s a simple concept that I avoid because I’ve never been given a task that requires a deep understanding of them. Whenever I encounter this issue, I simply use coroutine.yield() to resolve it, although I can’t explain why it fixes the problem.

A thread that is currently running can’t be cancelled, so there is an issue which is causing an attempted cancel while the thread is still running.

What does exit do? If it fires exitRequest, especially if it’s not the last thing it does, the thread will still be running. If you do fire it as the last thing, you’ve got a race condition, you need that thread to finish running before the connected function tries to cancel it.


this is a workaround, it only works because it will halt execution of the thread and put it into the yielded state. There’s no work active on it, so it can be cancelled (effectively killed, put into a dead state).

As other have correctly affirmed, this most likely because the thread you are in is the one you are trying to cancel. I typically use this idiom to cancel the running thread.

task.defer(task.cancel, thread)

This might be unnecessary in the future as per Roblox’s warnings. I’d hope it were sooner than later.

So how would I get around this?

That is very smart, and is the reason why it couldn’t cancel. I had to work around this by just making the signal fire only when the player is out of distance.

dialogueObject.outOfRange:Connect(function()
	if not responseThread then return end
	-- Only cancel if responseThread is a thread created by task.spawn
	if typeof(responseThread) == "thread" then
		task.cancel(responseThread)
	else
		print("responseThread is not a cancellable thread:", responseThread)
	end

	responseThread = nil
end)
repeat
		local distance = (npcPos - charPos).Magnitude
		
		if distance > maxDistance then
			self.outOfRange:Fire()
			self:exit("Goodbye!")
		end
		task.wait()
	until self.active == false