[SWEP] Having trouble to set a "cooldown" on my melee attack !

Hi everybody,

I come here because i have trouble adding a cooldown on my melee attack, i think my logic is the good one but i got animation problem with this code, if i delete the


if CurTime() > nextAttack then

and


END

the animation is fine but i have no cooldown :frowning:

If you need more detail i would be happy to give you more, if you want to see what the bug looks like i can send you my server’s ip to come and see it yourself, it’s hard to explain with words.

To be simple the animation is freezing with the loop



SWEP.MoneyGap = 2000
SWEP.Power = 2000
SWEP.HitDistance = 48
SWEP.WeaponDamage = 34
SWEP.CooldownBill = 0.5
SWEP.CooldownMelee = 1

function SWEP:PrimaryAttack()
		local nextAttack = self.Owner:GetNWFloat("nextAttackMelee")
		if CurTime() > nextAttack then
			print("Triggeredzz")
			self:MeleeAttack()
			self.Owner:SetNWFloat("nextAttackMelee", CurTime() + self.CooldownMelee)
		end
end

function SWEP:MeleeAttack()
	--On prend en compte la latence avec un LagCompensation,
	self.Owner:LagCompensation( true )


  -- Distance entre owner et cible
	local tr = util.TraceLine( {
		start = self.Owner:GetShootPos(),
		endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * self.HitDistance,
		filter = self.Owner,
		mask = MASK_SHOT_HULL
	} )

	-- Si la trace line/hull a rencontré une cible on joue un son.
	if ( tr.Hit && !( game.SinglePlayer() && CLIENT ) ) then
		--self:EmitSound(  )
	end

	local hit = false

	if ( SERVER && IsValid( tr.Entity ) && ( tr.Entity:IsNPC() || tr.Entity:IsPlayer() || tr.Entity:Health() > 0 ) ) then
		local dmg = DamageInfo()

		local attacker = self.Owner
		if ( !IsValid( attacker ) ) then attacker = self end
		dmg:SetAttacker( attacker )

		dmg:SetInflictor( self )
		dmg:SetDamage( self.WeaponDamage )

		tr.Entity:TakeDamageInfo( dmg )
		--self.Owner:TakeDamageInfo( dmg )
		hit = true

	end

  self.Owner:SetAnimation( PLAYER_ATTACK1 )
  self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )

	-- Ne pas oublier de le remettre à false car peux poser des problémes.
	self.Owner:LagCompensation( false )
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
	self.Owner:SetNWFloat("nextAttackBill", CurTime() + self.CooldownBill)
	self.Owner:SetNWFloat("nextAttackMelee", CurTime() + self.CooldownMelee)
	return true
end

Thanks in advance,

PS : If you want to add me to join the server and see it yourself add : AnEducatedPow

It might be good to use http://wiki.garrysmod.com/page/Weapon/SetNextPrimaryFire instead of


if CurTime() > nextAttack then

You can find some example code on this page: http://wiki.garrysmod.com/page/WEAPON/CanPrimaryAttack

EDIT : My bad IT WAS THE SOLUTION ! Thanks a lot buddy ! SOLVED !