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)
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)
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.