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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.