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)
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)
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.