This is resulting in the error whenever I switch between tools.
cannot resume dead coroutine
local function toolEquip()
print("equipingtool")
wait(5)
print("tool equiped")
end
local equipTask
char.ChildAdded:Connect(function(child)
if child.ClassName == "Tool" then
MainSettings.Equipped = true
tool = child
if child:FindFirstChild("Settings") then
setup()
equipTask = task.spawn(toolEquip)
end
end
end)
char.ChildRemoved:Connect(function(child)
if child.ClassName == "Tool" then
MainSettings.Equipped = false
tool = nil
if child:FindFirstChild("Settings") then
unsetup()
if equipTask then
task.cancel(equipTask)
end
end
end
end)
I can’t seem to figure out the reason why the task gives the error? The code itself works as intended and is supposed to let me stop certain sequences of actions whenever the tool is changed, unequipped or equipped.
It is probably due to the task being finished and you trying to cancel it. Currently, as far as I know, there is no way to check if a task is finished, so a workaround would be like this:
local equipTask
local function toolEquip()
print("equipingtool")
wait(5)
equipTask = nil
print("tool equiped")
end
char.ChildAdded:Connect(function(child)
if child.ClassName == "Tool" then
MainSettings.Equipped = true
tool = child
if child:FindFirstChild("Settings") then
setup()
equipTask = task.spawn(toolEquip)
end
end
end)
char.ChildRemoved:Connect(function(child)
if child.ClassName == "Tool" then
MainSettings.Equipped = false
tool = nil
if child:FindFirstChild("Settings") then
unsetup()
if equipTask then
task.cancel(equipTask)
end
end
end
end)
Turns out I just needed to replace the wait with a task.wait() instead. The code after the original wait would try to execute but the task would have been ended by then causing the game to be unable to resume anything.