Cooldown on hook KeyPress

I have an achievement for jumping 250000 times on my server but when too many players jump at once, it seems to cause lag and make the timer not work properly. I put a cooldown because I don’t want to make every actual keypress add to the achievement. The timer is accurate(ish) to when a player lands and is ready to jump again. Has anybody got a alternative solution that would work better than this? :



if SERVER then
	local jumpcool = 0
	hook.Add( "KeyPress", "ACV " .. "Keypress" .. ACVMT.LuaName, function( ply, key )
		if (key == IN_JUMP) and jumpcool == 0 then
			ply:ACV_Increase(ACVMT.LuaName,1)
			jumpcool = 1
			timer.Simple(0.55, function()
				jumpcool = 0
			end)
		end
	end)
end


Thanks is advance.

Don’t increase the achievement when the player is in air ( Player:IsOnGround() )

Thank you sir! Much appreciated.

Also, you want to change this code:



jumpcool = 1
			timer.Simple(0.55, function()
				jumpcool = 0
			end)

Into this:


ply.jumpcool = 1
			timer.Create("mytimer" .. ply:EntIndex(), 0.55, 1, function()
				ply.jumpcool = 0
			end)

So the variables and timers won’t get overridden by other players jumping and such.

Why not just call it when the player has landed?

Can be done Server side: This allows you to track when they land instead of using think hook to detect key-presses… If they land, they either jumped, or fell…
[lua]hook.Add( “OnPlayerHitGround”, “Stamina:SlowDownOnLang”, function( _p, bInWater, bOnFloater, flFallSpeed )

end );[/lua]

Edit: If you want to disable jump key after they land to prevent excessive jumping:

[lua]//
// Disable jump key while out of breath.
//
hook.Add( “CreateMove”, “Stamina:DisableJumpKey”, function( input )
if ( LocalPlayer( ):IsAdmin( ) ) then return; end
if ( input:KeyDown( IN_JUMP ) ) then
if ( LocalPlayer( ):GetJumpPower( ) < PLAYER_JUMP_POWER_MIN ) then
local _buttons = input:GetButtons();
input:SetButtons( _buttons - IN_JUMP )
end
end
end )[/lua]

Change the jump-power to the timer system and you should be good to go.