XPCall - Cannot resume dead coroutine

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

In Roblox, the “XPCall - Cannot resume dead coroutine” error typically occurs when a coroutine that has already completed is being resumed or when a coroutine that has not been started is being resumed. Here are a few potential ways to fix this error:

  1. Make sure you are only attempting to resume coroutines that are actually running or have not yet completed. If a coroutine has already completed, you will need to create a new one in order to continue using it.
  2. Check that you are correctly starting and yielding your coroutines. A common cause of this error is forgetting to yield the coroutine at some point, which can cause it to complete before it is intended to.
  3. Make sure you are not attempting to resume a coroutine from within itself. This can lead to an infinite loop and cause the error to occur.
  4. If you are using a try-catch block to handle errors, make sure you are not inadvertently catching and silently discarding the error. This can mask the underlying issue and make it difficult to diagnose and fix.
  5. If you have recently made changes to your code that may have caused this error to occur, try reverting to a previous version of your code to see if the issue is resolved. This can help you identify the specific cause of the error.

I hope this helps! Let me know if you have any further questions.

did you seriously use chatgpt on me?

Lol, you got me.

But did it help you though

no because i havent made any coroutines, although the background function is activated by spawn() so not sure if thats counts as a coroutine

just had to do spawn(background) again

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.