Gmod 13 Lua 错误

我正在尝试学习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)
                        -下一行错误-
         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()
    -下一行错误-
         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

我收到一个错误,该错误说“尝试索引字段'Weapon'(空值)”。有人能解释如何修复此问题吗?我不允许发布图像,这是我收到的错误图片。

点赞
用户631634
用户631634

原因:你之所以收到那个错误是因为"Weapon" (具体指self.Weapon)没有被初始化。self.Weapon指向空,因此您无法在其上调用任何函数。

您可以向我们展示引用错误消息的行吗?它似乎是在文件shared.lua的第84、85和90行。周围的代码也很有帮助。我猜您在原始问题中发布了它,但没有任何行号不是有用的!

2012-12-30 20:24:54