Player keeps regenerating after being damaged

I’m trying to make a delayed regeneration script where if the player isn’t damaged for 5 seconds, then he will start to regenerate. I have already disabled the default health regeneration script, and I’m trying to make it so that if the player gets damaged while he is regenerating, then he will have to wait another 5 seconds to regenerate again. Right now my script causes the player to continue regenerating forever including after being damaged and the regeneration effect stacks for some reason.

local inCombat = false

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local oldHealth = humanoid.Health
		
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth or inCombat == true then
				local newHealth = humanoid.Health
				inCombat = true
				wait(5)
				
				if humanoid.Health >= newHealth then
					inCombat = false
				end

				if inCombat == false then
					repeat
						humanoid.Health = humanoid.Health + 5
						wait(0.5)
					until humanoid.Health >= humanoid.MaxHealth
				end

			end
		end)

	end)
end)
1 Like

Try this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local inCombat = false

		local humanoid = character:WaitForChild("Humanoid")
		local oldHealth = humanoid.Health

		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth or inCombat then
				local newHealth = humanoid.Health
				inCombat = true
				
				task.wait(5)

				if humanoid.Health >= newHealth then
					inCombat = false
				end
				
				while not inCombat do
					humanoid.Health += 5
					
					if humanoid.Health >= humanoid.MaxHealth then
						break
					end

					task.wait(0.5)
				end
			end
		end)
	end)
end)

Thanks, but the script makes it that now the player regenerates every 5 seconds. It might probably be repeating the whole thing.

The problem is that you have a repeat statement that never checks whether the player is back into the combat state. Also, I would recommend using a different method to change the inCombat to false.

local tookdamage = 0
local inCombat = true

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local oldHealth = humanoid.Health
		
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth then
				tookdamage = tick()
				inCombat = true
			end
		end)
	end)
	
	while true do
		if tookdamage > 0 or inCombat then
			if tick()-tookdamage >= 5 then
				tookdamage = 0
				inCombat = true
			end
		end
		
		if not inCombat and player.Character then
			if player.Character.Humanoid.Health > 0 then
				player.Character.Humanoid.Health += 5
				task.wait(0.5)
			end
		end
	end
end)

Hi again, I do like your method for checking inCombat. I tested the script and changed a few minor things, but the player still regenerates every 5 seconds for some reason.

Oh, the problem is that oldHealth never changes.

local tookdamage = 0
local inCombat = true

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local oldHealth = humanoid.Health
		
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth then
				tookdamage = tick()
				inCombat = true
			end
			
			oldHealth = humanoid.Health
		end)
	end)
	
	while true do
		if tookdamage > 0 or inCombat then
			if tick()-tookdamage >= 5 then
				tookdamage = 0
				inCombat = true
			end
		end
		
		if not inCombat and player.Character then
			if player.Character.Humanoid.Health > 0 then
				player.Character.Humanoid.Health += 5
				task.wait(0.5)
			end
		end
	end
end)

I’m not sure why, but the regeneration isn’t working now.

That’s weird, are there any errors?

Yeah, there weren’t any errors.

Oh, well, I see why. I set inCombat to true after the time was up lmao

local tookdamage = 0
local inCombat = true

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local oldHealth = humanoid.Health
		
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth then
				tookdamage = tick()
				inCombat = true
			end
			
			oldHealth = humanoid.Health
		end)
	end)
	
	while true do
		if tookdamage > 0 or inCombat then
			if tick()-tookdamage >= 5 then
				tookdamage = 0
				inCombat = false
			end
		end
		
		if not inCombat and player.Character then
			if player.Character.Humanoid.Health > 0 then
				player.Character.Humanoid.Health += 5
				task.wait(0.5)
			end
		end
	end
end)

I noticed that lol and I changed a bit of things, here is my current script (it still doesn’t work though):

local timesincedmg = 0
local inCombat = false

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local oldHealth = humanoid.Health

		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth then
				timesincedmg = tick()
				inCombat = true
			end
		end)

		if timesincedmg > 0 or inCombat then
			if tick() - timesincedmg >= 5 then
				timesincedmg = 0
				inCombat = false
			end
		end

		while not inCombat do
			if humanoid.Health > 0 then
				humanoid.Health += 5
				task.wait(0.5)
			end
		end

	end)
end)

I completely see why it doesn’t work now. It no longer loops at all, AND you add it into the CharacterAdded function. I had it outside of the character added function to save performance and since it doesn’t loop it no longer does the check.

local timesincedmg = 0
local inCombat = false

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local oldHealth = humanoid.Health

		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth then
				timesincedmg = tick()
				inCombat = true
			end
		end)
	end)

	while task.wait() do
		if timesincedmg > 0 or inCombat then
			if tick() - timesincedmg >= 5 then
				timesincedmg = 0
				inCombat = false
			end
		end
		
		if not inCombat and player.Character then
			if player.Character.Humanoid.Health > 0 and player.Character.Humanoid.Health < 100 then
				humanoid.Health += 5
				task.wait(0.5)
			end
		end
	end
end)

Ok I tested it and sorry to say but it still doesn’t work, I think I should try figuring out the problem myself if I’m taking too much of your time. Thanks for the help though.

I got it working, the problem this time was that I didn’t replace humanoid inside of the loop.

local timesincedmg = 0
local inCombat = false

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local oldHealth = humanoid.Health

		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth then
				timesincedmg = tick()
				inCombat = true
			end
		end)
	end)

	while task.wait() do
		if timesincedmg > 0 or inCombat then
			if tick() - timesincedmg >= 5 then
				timesincedmg = 0
				inCombat = false
			end
		end

		if not inCombat and player.Character then
			if player.Character.Humanoid.Health > 0 and player.Character.Humanoid.Health < 100 then
				player.Character.Humanoid.Health += 5
				task.wait(0.5)
			end
		end
	end
end)

The regen is now working, but it’s back to every 5 seconds again rip.

Wow, idk how, but I keep reverting the fixes I make when changing your version lol

local timesincedmg = 0
local inCombat = false

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local oldHealth = humanoid.Health

		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health < oldHealth then
				timesincedmg = tick()
				inCombat = true
			end
			
			oldHealth = humanoid.Health
		end)
	end)

	while task.wait() do
		if timesincedmg > 0 or inCombat then
			if tick() - timesincedmg >= 5 then
				timesincedmg = 0
				inCombat = false
			end
		end

		if not inCombat and player.Character then
			if player.Character.Humanoid.Health > 0 and player.Character.Humanoid.Health < 100 then
				player.Character.Humanoid.Health += 5
				task.wait(0.5)
			end
		end
	end
end)
2 Likes

Oh my gosh it works now, thank you. Yeah I shouldn’t have changed your script, thought I could fix some things when I don’t even know much of scripting lol.

Ah, it’s fine, trying to figure things out yourself is actually a good thing. It gives you some good experience.

1 Like