0

I am trying to learn Lua and I decided that for my first project I would try to fix a broken script. I have fixed a few of the bugs but I'm stuck now. Can you help me?

    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

I'm getting an error saying Attempting to index field 'Weapon' (a nil value). Can anyone explain how to fix this?

Im not allowed to post imagesthis is what im getting Error image

ASCIIThenANSI
  • 865
  • 1
  • 9
  • 27
Bob Joe
  • 1
  • 2

1 Answers1

0

The reason you're getting that error is that "Weapon" (specifically self.Weapon) was not initialized. self.Weapon points to nothing, so you can't call any functions on it.

Can you show us the lines that the error messages reference? It looks like the file is shared.lua, lines 84, 85 and 90. The surrounding code would be helpful, too. I'm guessing that you posted it as part of your original question, but it's not helpful without any line numbers!

T Suds
  • 317
  • 2
  • 7