Need help with gradual player health loss

Honestly I am completely stumped by this. I am trying to make a system where after a player is downed, they gradually loose health until they die, or until they are revived by another player. Problem is, the script is malfunctioning and it will loose health by one, then two, then six, then the player is dead. H e l p.

character.Humanoid.HealthChanged:Connect(function()
			if character.Humanoid.Health <= 15 then
				character:FindFirstChild("Health").Disabled = true
				
				while character.Humanoid.Health <= 15 do
					local dt = wait(2)
					character.Humanoid.Health = math.min(character.Humanoid.Health - 1, character.Humanoid.MaxHealth)
					character.Humanoid.HealthChanged:Wait()
				end
				
			elseif character.Humanoid.Health > 15 then
				character:FindFirstChild("Health").Disabled = false
				
			end
		end)
1 Like

Draining the health of the player will continually fire the function again and again. So, you should check if the player is already losing health.

isLosingHealth = false

character.Humanoid.HealthChanged:Connect(function()
			if character.Humanoid.Health <= 15 and isLosingHealth == false then
				isLosingHealth = true
				character:FindFirstChild("Health").Disabled = true
				
				while character.Humanoid.Health <= 15 do
					local dt = wait(2)
					character.Humanoid.Health = math.min(character.Humanoid.Health - 1, character.Humanoid.MaxHealth)
					character.Humanoid.HealthChanged:Wait()
				end
				
			elseif character.Humanoid.Health > 15 then
				character:FindFirstChild("Health").Disabled = false
				
			end
		end)

And then elsewhere, something can set isLosingHealth to false when they are revived.

It only goes down one health and then stops. Idk if i did something wrong

Edit:NVM

local oldHealth = humanoid.Health

humanoid.HealthChanged:Connect(function(newHealth)
	if newHealth <= oldHealth then
		return
	else
		oldHealth = newHealth
		--Do code.
	end
end)