简体   繁体   English

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

[英]How to fix cooldown for Garry's Mod weapon? (attempt to compare nil with number)

I'm making a weapon in Garry's Mod that has three functions that use both mouse buttons and the R key.我正在 Garry's Mod 中制作一种武器,它具有三个功能,可同时使用鼠标按钮和 R 键。 Since Garry is cool, I was able to easily set the delay for the mouse button attacks with SetNextPrimaryFire() and SetNextSecondaryFire().由于 Garry 很酷,我可以使用 SetNextPrimaryFire() 和 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.如果我从 SWEP:Think() 下方的第一行中删除“and self.ultdelay <= CurTime()”,则代码工作正常,但不适用所需的 15 秒延迟,每次按下 R 时该函数都会运行. 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当它存在时,该函数完全停止工作并导致 [错误] lua/weapons/lucian/shared.lua:103: 尝试将 nil 与数字 1 进行比较。未知 - lua/weapons/lucian/shared.lua:103

Try changing line 103 to this:尝试将第 103 行更改为:

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.您需要这样做的原因是因为您没有在 SWEP:Initialize ultdelay设置为任何值,因此它会尝试比较尚未设置的值,因此会出现错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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