Problem whit timers

Hello guys

I create a health regen for a deathmatch gamemode but when a player get a damage its detroy the timer for everyone,
i know what its make this but not how to remedy that.



function GM:EntityTakeDamage( target, dmginfo )
	for k, v in ipairs( player.GetAll( ) ) do
		timer.Simple(0.1, function()
			if IsValid(v) then
				x_health = 100 - v:Health()
				x_timer = ( x_health / 10 + 0.1)
			end
		end)
		
		timer.Create( "TimeHealth", 2, 1, function() 
	
			--target:EmitSound( "items/smallmedkit1.wav" )
			timer.Create( "GetHealth", 0.1, 0, function()
				v:SetHealth(v:Health() + 1)
			end)
		
			timer.Simple( x_timer , function()
				timer.Destroy( "GetHealth" )
			end)
		end)
		timer.Destroy( "GetHealth" )
	end
end


Why not create a timer on spawn and check if the playwr is alive and reached max hp then stop regen.

It does not answer my question

I don’t think it will help - But you should be using timer.Remove() rather than timer.Destroy(). This is because it is deprecated. It does the exact same thing though.

You are also making the same timer for every players, so its overriding itself so it won’t delete itself. I would do something like this to overcome it


function GM:EntityTakeDamage( target, dmginfo )

	local timernamenumber = 0 -- SO ITS A VALUE     --         [BINGS EDIT]

	for k, v in ipairs( player.GetAll( ) ) do
		timernamenumber = timernamenumber + 1 -- SO IT CHANGES EVERY PLAYER, COULD BE DONE FURTHER DOWN SO 0 IS USED BUT DOESN'T MATTER         --           [BINGS EDIT]
		timer.Simple(0.1, function()
			if IsValid(v) then
				x_health = 100 - v:Health()
				x_timer = ( x_health / 10 + 0.1)
			end
		end)
		
		timer.Create( 'TimeHealth'..timernamenumber..'timer', 2, 1, function()   --    [BINGS EDIT]
	
			--target:EmitSound( "items/smallmedkit1.wav" )
			timer.Create( 'GetHealth'..timernamenumber..'timer', 0.1, 0, function()  --   [BINGS EDIT]
				v:SetHealth(v:Health() + 1)
				timer.Remove( 'GetHealth'..timernamenumber..'timer' ) -- MOVED HERE      [BINGS EDIT]
			end)
		
			timer.Simple( x_timer , function()
				timer.Remove( 'GetHealth'..timernamenumber..'timer' )  --     [BINGS EDIT]
			end)
		end)
-- 		timer.Destroy( 'GetHealth'..timernamenumber..'timer' ) WHY HERE?              [BINGS EDIT]
	end
end

It hasn’t been tested but should help your problem :slight_smile: I also but [BINGS EDIT] on every line I changed/added/–

Thank you, I had not thought that, I changed my code and its work now



function GM:EntityTakeDamage( target, dmginfo )

	for k, v in ipairs( player.GetAll( ) ) do
		timer.Simple(0.1, function()
			if IsValid(v) then
				x_health = 100 - target:Health()
				x_timer = ( x_health / 10 + 0.1)
			end
		end)
		
		timer.Create( "TimeHealth "..target:Nick(), 2, 1, function() 
			target:EmitSound( "items/smallmedkit1.wav" )
			timer.Create( "GetHealth "..target:Nick(), 0.1, 0, function()
				target:SetHealth(target:Health() + 1)
			end)
		
			timer.Simple( x_timer , function()
				timer.Remove( "GetHealth "..target:Nick() )
			end)
		end)
	end
	timer.Remove( "GetHealth "..target:Nick() )
end