【问题标题】:Gmod 13 Lua ErrorGmod 13 Lua 错误
【发布时间】:2015-04-19 05:04:28
【问题描述】:

我正在尝试学习 Lua,我决定在我的第一个项目中尝试修复损坏的脚本。我已经修复了一些错误,但我现在卡住了。你能帮帮我吗?

    function SWEP:PrimaryAttack()
     if( CurTime() < self.NextStrike ) then return; end
     self.Weapon:EmitSound("player/skick/sparta.mp3")
     self.NextStrike = ( CurTime() + 3.5 );
     timer.Simple( 1.80, function() self:AttackAnim() end)
                        -Next line broken-
         timer.Simple( 2.40, function() self.Weapon:SendWeaponAnim( ACT_VM_IDLE ) end);
     timer.Simple( 2.00, function() self.ShootBullets( self ) end)
     self.Owner:SetAnimation( PLAYER_ATTACK1 );
end 

function SWEP:ShootBullets()
    -Next line Broken-
         local trace =Owner:GetEyeTrace();
    if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 130 then
        if( trace.Entity:IsPlayer() or trace.Entity:IsNPC() or trace.Entity:GetClass()=="prop_ragdoll" ) then
                timer.Simple(0, game.ConsoleCommand, "host_timescale 0.1\n")
                timer.Simple(0.5, game.ConsoleCommand, "host_timescale 1\n")
            self.Owner:EmitSound( self.FleshHit[math.random(1,#self.FleshHit)] );
        else
            self.Owner:EmitSound( self.Hit[math.random(1,#self.Hit)] );
        end
                bullet = {}
                bullet.Num    = 5
                bullet.Src    = self.Owner:GetShootPos()
                bullet.Dir    = self.Owner:GetAimVector()
                bullet.Spread = Vector(0.04, 0.04, 0.04)
                bullet.Tracer = 0
                bullet.Force  = 250
                bullet.Damage = 1000000
            self.Owner:FireBullets(bullet)
    end

我收到一条错误消息,提示 Attempting to index field 'Weapon' (a nil value)。 谁能解释一下如何解决这个问题?

我不允许发布图片这是我得到的Error image

【问题讨论】:

  • 我喜欢 GMOD,祝你好运!
  • 您可以使用self:AttackAnim() 代替self.AttackAnim(self)。这就是冒号 (:) 的全部意义所在。你确定它不会在第一次使用Weapon 字段时提前崩溃吗?
  • 谢谢我试一试,但不知道为什么我会得到这个错误?

标签: lua garrys-mod


【解决方案1】:

您收到该错误的原因是“武器”(特别是 self.Weapon)未初始化。 self.Weapon 没有指向任何东西,所以你不能在它上面调用任何函数。

您能告诉我们错误消息引用的行吗?看起来文件是shared.lua,第 84、85 和 90 行。周围的代码也会有所帮助。我猜您将其作为原始问题的一部分发布,但没有任何行号就没有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2021-04-09
    • 2023-02-18
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多