1

I'm making a weapon in Garry's Mod that has three functions that use both mouse buttons and the R key. Since Garry is cool, I was able to easily set the delay for the mouse button attacks with SetNextPrimaryFire() and SetNextSecondaryFire(). Unfortunately, there's no convenient function like those set up for other keys. So, a stranger suggested that I try this.

function SWEP:SetNextUltFire(time)
    self.ultdelay = time
end

function SWEP:Think()

    if self.Owner:KeyPressed( IN_RELOAD ) and self.ultdelay <= CurTime() then
        walkspeed = 800
        runspeed = 800
        self:EmitSound(self.WeaponSounds2[math.random(1,#self.WeaponSounds2)], 100, 100)
        self.Owner:SetWalkSpeed(800);self.Owner:SetRunSpeed(800)
        firerate = 0.15
        timer.Create("stopult", 10, 1, function()
            self.Owner:SetWalkSpeed(250);self.Owner:SetRunSpeed(500);
            firerate = 0.3; self:SendWeaponAnim( ACT_VM_RELOAD );self:SetNextPrimaryFire( CurTime() + 2.8 );
            walkspeed = 250; runspeed = 500 end)
        self:SetNextUltFire(CurTime()+15)
    end
end

if I remove "and self.ultdelay <= CurTime()" from the first line below SWEP:Think(), the code works just fine, but the desired 15 second delay doesn't apply, the function runs every time R is pressed. When it is present, the function stops working altogether and results in [ERROR] lua/weapons/lucian/shared.lua:103: attempt to compare nil with number 1. unknown - lua/weapons/lucian/shared.lua:103

pppery
  • 3,731
  • 22
  • 33
  • 46
Isaac York
  • 11
  • 2
  • Where is CurTime() being defined? If ultdelay is defined through the function, and you're sure it's a number, the only thing left is the CurTime(), which may be set to nil. – Josh Oct 02 '13 at 23:03

1 Answers1

0

Try changing line 103 to this:

if self.Owner:KeyPressed( IN_RELOAD ) and (not self.ultdelay or self.ultdelay <= CurTime()) then

The reason you need to do this is because you're not setting ultdelay to any value in SWEP:Initialize, so it's trying to compare a value that hasn't been set, hence the error message.

Gran PC
  • 21
  • 3