crewData.Task = task.delay(TimeToAttack, function()
while crewData.Target == SelectedTarget do
local Alive = GameService:Attack(player, SelectedTarget, crewId)
if not Alive then -- NPC died
self:Recall(player)
break
end
task.wait(1)
end
end)
-- later on
if crewData.Task then
task.cancel(crewData.Task) -- ERROR
end
You just have to find the cause by commenting out certain parts of the code.
For example, after commenting something out I can be sure that the thread is cancelled correctly, so, in your case, that is not the problem.
It might be the case that the thread has died before using task.cancel
An easy fix would be to use coroutine.close instead, which doesn’t error if you attempt to close a dead thread but rather returns false and a string value containing the error message