Cannot resume dead coroutine

Hi! I have a script that will display a Gui with no transparency and slowly turn the transparency up when the player takes damage.
But the issue is that the error in the title “Cannot resume dead coroutine” repeats while the player is taking damage.
How can this be fixed? Here’s the code.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local hurtGui = player.PlayerGui:WaitForChild("ScreenGui").Damage

local currentHealth = humanoid.Health

local thread

function HealthGui()
	hurtGui.ImageTransparency = 0
	repeat
		wait(0.1)
		hurtGui.ImageTransparency += 0.02
	until hurtGui.ImageTransparency == 1
	thread = nil
end

humanoid.HealthChanged:Connect(function(newHealth)
	if currentHealth > newHealth then
		hurtGui.Visible = true
		if thread then
			task.cancel(thread)
			thread = nil
		end
		thread = task.spawn(HealthGui)
		currentHealth = newHealth
	end
end)

Is it a glitch?

1 Like

i don’t think it’s a serious bug that will break your code, I think it’s just a warning saying that the thread cannot continue because it got canceled

2 Likes

When the HealthChanged event fires, it attempts to cancel the previous coroutine, but since it has already been completed and thread is nil, the error occurs when trying to cancel it.

Try this.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local hurtGui = player.PlayerGui:WaitForChild("ScreenGui").Damage

local currentHealth = humanoid.Health
local coroutineRunning = false

function HealthGui()
    hurtGui.ImageTransparency = 0
    repeat
        wait(0.1)
        hurtGui.ImageTransparency += 0.02
    until hurtGui.ImageTransparency >= 1
    coroutineRunning = false
end

humanoid.HealthChanged:Connect(function(newHealth)
    if currentHealth > newHealth then
        hurtGui.Visible = true
        if not coroutineRunning then
            coroutineRunning = true
            task.spawn(HealthGui)
        end
        currentHealth = newHealth
    end
end)
1 Like

he checked if the thread is nil or not

1 Like

i think the error occurs when the script tries to cancel the thread when the repeat statement was running

1 Like

I want to have the function get overwritten. So when it is running and it gets called, it will restart the function.

1 Like

I’m currently not using Roblox Studio so as of writing this I don’t really have a way to test anything. But if you originally planned to make a coroutine, discard it using comments and try this.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local hurtGui = player.PlayerGui:WaitForChild("ScreenGui").Damage

local currentHealth = humanoid.Health

local function HealthGui()
	hurtGui.ImageTransparency = 0
	repeat
		wait(0.1)
		hurtGui.ImageTransparency += 0.02
	until hurtGui.ImageTransparency == 1
end

humanoid.HealthChanged:Connect(function(newHealth)
	if currentHealth > newHealth then
		hurtGui.Visible = true
		--Spawn(HealthGui)
        HealthGui()
		currentHealth = newHealth
	end
end)

See if this gives you the intended results. Spawn(HealthGui) use this if the current HealthGui() doesn’t you the intended results.

1 Like

Thanks! It worked! I used Spawn(HealthGui).

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