How to check if the players health went down, and not up

So I’m trying to make an “in combat” script, so when the player gets damaged he will be “in combat” for 10 seconds. I first did this by using Humanoid.HealthChanged, but the problem is that even when the timer goes out if the player doesn’t have max health and is healing the function will activate again, even though the player is not in combat.
This is a part of the localscript:

local player = game.Players.LocalPlayer
local hum = game.Workspace:WaitForChild(player.Name):WaitForChild("Humanoid")
local incombat = false

hum.HealthChanged:Connect(function(newHealth)

	if incombat == false then
		print (newHealth)
		incombat = true
		wait (10)
		incombat = false
	end
  end)
1 Like
local player = game.Players.LocalPlayer
local hum = game.Workspace:WaitForChild(player.Name):WaitForChild("Humanoid")
local incombat = false

local currentHealth = hum.Health
hum.HealthChanged:Connect(function(newHealth)
	if newHealth > currentHealth then -- if healing, health increased
		currentHealth = newHealth --update current health
		return --skip
	end
	currentHealth = newHealth

	if incombat == false then
		print(newHealth)
		incombat = true
		wait(10)
		incombat = false
	end
end)

6 Likes

Check if the health before is greater than the new health by storing it in a variable. If it is, make the player “in combat”

Thanks! But this for some reason stops working once the character has died once, do you know how to fix that?

The issue is that the script only runs once. Put it in starter character scripts or use player character added to get the new humanoid the player gets after respawning which will need the health changed connection again.

1 Like