2013-10-02 63 views
1

我在Garry的Mod中製作了一個武器,它有三個使用鼠標鍵和R鍵的功能。由於Garry很酷,我可以使用SetNextPrimaryFire()和SetNextSecondaryFire()方便地設置鼠標按鍵攻擊的延遲。不幸的是,沒有爲其他鍵設置的功能。所以,一個陌生人建議我嘗試這個。LUA如何解決Garry的Mod武器的冷卻時間? (試圖比較無編號)

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 

如果刪除「和self.ultdelay < = CURTIME()」從下方SWEP的第一行:認爲(),代碼工作得很好,但所期望的15秒延遲不適用,該函數每次按下R時都會運行。當它存在時,該功能完全停止工作並導致 [ERROR] lua/weapons/lucian/shared.lua:103:嘗試與零比較nil 1. unknown - lua/weapons/lucian/shared.lua: 103

有什麼建議嗎?

+0

CurTime()被定義在哪裏?如果ultdelay是通過函數定義的,並且您確定它是一個數字,則唯一剩下的就是CurTime(),它可以設置爲nil。 – Josh

回答

0

嘗試改變線103這樣的:

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

你需要做的原因是因爲你沒有設置ultdelay在舒瑞普的任何值:初始化,所以它試圖來比較不是招的值沒有設置,因此錯誤信息。