Creating a bleeding system

When the player gets hit, they begin bleeding for some time, however, it takes some time for the player to stop bleeding. Because of this and the fact that players can get hit multiple times, multiple loops will be created when the player is hit multiple times, resulting in this problem where the different loops will interrupt each other when setting the health of the player.

What I need to do is, if the player gets hit again, the script should do the following things:

  1. Stop the current loop when player is hit again
  2. Add the damage from the previous loop to the damage of the new loop, so stacking the damage on every hit

Here’s a simplified example of what I’m doing (server side)

-- Remote event fires to server
	local co = coroutine.create(function()
		local Diff = CurrentBlood - NewBlood
		for blood = CurrentBlood, NewBlood, -1 do
			health = blood
			
			task.wait(BleedRate / Diff)
		end
	end)
	
	coroutine.resume(co)

Bump

You can check the status of the existing coroutine. You should also add bleedAmount and increase it if the coroutine is still running, so the current coroutine causes the character to bleed more.