繁体   English   中英

如何修复 Garry 的 Mod 武器的冷却时间? (尝试将 nil 与数字进行比较)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM