Instantly printing "DEAD"

I’m trying to make a death crawl system, which when you die, you start bleeding out, and your health decreases by -15 every 2 seconds till you die.

You get a crawl animation which you can crawl around till you die. Once you die it prints “DEAD”.

For some reason as soon as I set my health to 0 it says DEAD in the console.

local Hum = script.Parent:FindFirstChildOfClass("Humanoid")
Hum:SetStateEnabled(Enum.HumanoidStateType.Dead, false) -- makes sure player doesnt actually die at 0 health

local bleedingOut = false
local dead = false
local anim = Hum:FindFirstChild("Animator"):LoadAnimation(script.Crawl)
local deadAnim = Hum:FindFirstChild("Animator"):LoadAnimation(script.Dead)

Hum:GetPropertyChangedSignal("Health"):Connect(function()
	
	if dead then return end
	
	if Hum.Health <= 0 then
		if bleedingOut then
			print("DEAD")
			dead = true
			bleedingOut = false
		else
			deadAnim:Play()
			Hum.JumpHeight = 0
			bleedingOut = true
			Hum.WalkSpeed = 3
			Hum.Health = 60
		end
	else
		if bleedingOut and Hum.Health <= 60 then
			while bleedingOut do
				Hum.Health = Hum.Health - 15
				task.wait(2)
			end
		end
	end
end)

Hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if bleedingOut then
		if Hum.MoveDirection.Magnitude > 0 then
			deadAnim:Stop()
			anim:Play()
		else
			deadAnim:Play()
			anim:Stop()
		end
	end
	-- MoveDirection is a vector, specifically a unit vector to keep in mind
	-- and as a result of that, we can access its magnitude to get the
	-- 'speed' of that vector
end)
4 Likes

I’m not sure but I have a hunch that its doing what you’re telling it to do:

1 Like

I have a check to see if the player is bleeding out, and as soon as it starts bleeding out the health is set to 60 so it shouldnt be 0

1 Like

Okay but thats not what you said in this sentence:

1 Like

I’m not sure, but won’t multiple while loops end up running in the same time?

1 Like

At the beginning local bleedingOut = false, but in your script you define

if bleedingOut then
			print("DEAD")
			dead = true
			bleedingOut = false

Both “bleedingout” is false??

1 Like

Okay, you guys arent understanding.

BleedingOut is set to false because the player IS ALIVE.
When the players health becomes 0, it checks if the player is already bleeding out, if so, it means the player has bled out fully to death.

Otherwise, if the player isnt already bleeding out, it sets their health to 60, and slowly decreases their health by 15 every 2 seconds, and sets BleedingOut to true, so when the player’s health becomes 0 again the code above runs which prints “DEAD”

But it’s printing dead as soon as I set my health to 0 for some reason.

Please read all the code instead of trying to make sense of it with a few lines.

1 Like

Did you check if the health actually becomes 60? I suspect that the while loop ends up repeating multiple times causing it to instantly become 0.
To test this, I recommend you either decrease health after 2 seconds instead of before, or print before the loop starts.

3 Likes

Thanks, I had to add a debounce due to the loop running multiple times.

2 Likes

Instead of using health to detect if the player should be downed, make a separate module for doing damage that way the player gets downed in a more accurate way.

m.CanDamage = function(Humanoid: Humanoid , Damage : number, ShouldDown: boolean, OnHit: (Humanoid) -> any)
	if not Humanoid then
		return
	end
	if Damage >= Humanoid.Health and ShouldDown then
		OnHit(Humanoid)
		return
	end
	Humanoid:TakeDamage(Damage)
	OnHit(Humanoid)
end

m.CanDamage(Humanoid, 20, true, function(Humanoid)
	 -- Do something when player gets hit.
end)
2 Likes