Overwrite a function

Hey! I’d like to have a function that would be overwritten when it is called again.

The issue is that I can’t find anything to help me.

I have tried using coroutines but can’t figure out how they work in lua.

My code is here

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

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
		HealthGui()
		currentHealth = newHealth
	end
end)
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)

Thanks! I’ll try it tomorrow. I don’t have time to do it right now.

I get the error: cannot resume dead coroutine
Any way I can fix this?

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