Timer Problem

So I have coded a cooldown onto a swep with timers, But the problem is that the timers are global and someone can start the cooldown then everyone has the cooldown, so I posted here asking if there was a way to make timers local and only apply for that certain player that activated the timer, here is my code if you need it:


		if timer.Exists("Cooldown") then DarkRP.notify( ply, 1, 3, "You must wait " .. math.Round(timer.TimeLeft( "Cooldown" )) .. " seconds until you can use this again!" ) return false
			end
				SN_KnockOut.KnockOut(tr, ply)
				self.Owner:EmitSound("physics/body/body_medium_impact_hard"..table.Random(sounds_hit)..".wav")
				timer.Create( "Cooldown", SN_KnockOut["Settings"].cooldown or 5, 1, function() timer.Remove("Cooldown") end )
				timer.Start("Cooldown")
				return

An easy fix is using the EntityID in the timer name

But, this could be way more efficient using CurTime instead of timers.

How could I use Curtime ?

Something like this
[lua]
function DoStuff(ply)
ply.LastTimeIDidStuff = ply.LastTimeIDidStuff or 0
if CurTime() - ply.LastTimeIDidStuff < 1 then return false end – Seconds
ply:DoYourStuff()
ply.LastTimeIDidStuff = CurTime()
return true
end
[/lua]

I adapted to your code, this should work, just replace your current code.



if CurTime() - (SN_KnockOut.lastDeploy or 0) >= (SN_KnockOut["Settings"].cooldown or 5) then DarkRP.notify( ply, 1, 3, "You must wait " .. math.Round(CurTime() - SN_KnockOut.lastDeploy) .. " seconds until you can use this again!" ) return false end
SN_KnockOut.KnockOut(tr, ply)
self.Owner:EmitSound("physics/body/body_medium_impact_hard"..table.Random(sounds_hit)..".wav")
SN_KnockOut.lastDeploy = CurTime()
return


This code does not work and I want to know how Curtime works so I can use it in the future for something else.

From the GMod Wiki (http://wiki.garrysmod.com/page/Global/CurTime):

Returns the uptime of the server in seconds (to at least 4 decimal places)

Note: this is a synchronised value and affected by various factors such as host_timescale and the server being paused - either by sv_pausable or all players disconnecting.

You should use this function for timing in-game events but not for real-world events.

Also Anon I have tried that code and this is the error I got:


[ERROR] addons/knockoutswep/lua/weapons/knockout/shared.lua:246: '(' expected near 'SWEP'
  1. unknown - addons/knockoutswep/lua/weapons/knockout/shared.lua:0

Errr, there is no ‘SWEP’ in that code so it’s unrelated, post the code. and not just the line, post a few lines before and after.

I found out what the error was nad fixed it, also this is inside the swep primary attack function so could that be something?