[英]LUA How to fix Garry's Mod weapon? (attempt to compare nil with number)
[英]How to fix cooldown for Garry's Mod weapon? (attempt to compare nil with number)
我正在 Garry's Mod 中制作一種武器,它具有三個功能,可同時使用鼠標按鈕和 R 鍵。 由於 Garry 很酷,我可以使用 SetNextPrimaryFire() 和 SetNextSecondaryFire() 輕松設置鼠標按鈕攻擊的延遲。 不幸的是,沒有像為其他鍵設置的那樣方便的功能。 所以,一個陌生人建議我試試這個。
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
如果我從 SWEP:Think() 下方的第一行中刪除“and self.ultdelay <= CurTime()”,則代碼工作正常,但不適用所需的 15 秒延遲,每次按下 R 時該函數都會運行. 當它存在時,該函數完全停止工作並導致 [錯誤] lua/weapons/lucian/shared.lua:103: 嘗試將 nil 與數字 1 進行比較。未知 - lua/weapons/lucian/shared.lua:103
嘗試將第 103 行更改為:
if self.Owner:KeyPressed( IN_RELOAD ) and (not self.ultdelay or self.ultdelay <= CurTime()) then
您需要這樣做的原因是因為您沒有在 SWEP:Initialize ultdelay
設置為任何值,因此它會嘗試比較尚未設置的值,因此會出現錯誤消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.