Hey, I’m getting the error: cannot resume dead coroutine
- This happens when using the xpcall function to catch an error, when an error is caught, it starts a function called inter() - to enable the games intermission system to re-start the whole game loop. I’m calling xpcall function to make sure that if a player leaves when the second they won a match - the game won’t break
How would I go about fixing this, I need to call the function once the error is detected, any additional questions please do ask, and thanks for providing me great support.
Code
function background()
local successed, returnedData = xpcall(function()
while wait() do
if #playing == 1 then
variable = false
storage.Visible.Value = false
wait(3)
local char = playing[1].Character
if playing[1] == nil then
storage.Intermission.Value = "Nobody has won..."
start = false
wait(6)
storage.Intermission.Value = ""
playing = nil
playing = {}
inter()
return
end
playing[1].Character:WaitForChild("Humanoid"):UnequipTools()
local inventory = playing[1].Backpack
for i,v in pairs(inventory:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
end
end
local humz = playing[1].Character:FindFirstChild("Humanoid")
if humz then
humz.HeadScale.Value = 1
humz.BodyDepthScale.Value = 1
humz.BodyWidthScale.Value = 1
humz.BodyHeightScale.Value = 1
end
char.Humanoid.WalkSpeed = 19
char.Humanoid.JumpPower = 7.2
char:MoveTo(game.Workspace.Spawns.SpawnLocation.Position)
storage.Visible.Value = false
storage.Intermission.Value = tostring(playing[1]) .. " has won!"
game.Workspace.map.name.SurfaceGui.TextBox.Text = tostring(playing[1])
local name = tostring(playing[1])
print(name)
local bigger = 5
local humanoid = game.Workspace:WaitForChild("Winner")
local playerID = game.Players:GetUserIdFromNameAsync(name)
local HumanoidDesc = game.Players:GetHumanoidDescriptionFromUserId(playerID)
humanoid:WaitForChild("HumanoidRootPart").Anchored = false
humanoid.Humanoid:ApplyDescription(HumanoidDesc)
local find = humanoid:FindFirstChild("Humanoid")
if find then
find.HeadScale.Value *= bigger
find.BodyDepthScale.Value *= bigger
find.BodyWidthScale.Value *= bigger
find.BodyHeightScale.Value *= bigger
end
playing[1].leaderstats.Wins.Value +=1
start = false
wait(2)
humanoid:WaitForChild("HumanoidRootPart").Anchored = true
wait(5)
storage.Intermission.Value = ""
playing = nil
playing = {}
inter()
return
elseif #playing == 0 then
variable = false
storage.Visible.Value = false
storage.Intermission.Value = "Nobody has won..."
start = false
wait(7)
storage.Intermission.Value = ""
playing = nil
playing = {}
inter()
return
end
end
end, function(err)
variable = false
storage.Visible.Value = false
storage.Intermission.Value = "Nobody has won..."
start = false
wait(7)
storage.Intermission.Value = ""
playing = nil
playing = {}
inter()
return
end)
end