ERROR attempt to call method 'Team' <a nil value>

Hey guys, I am making an experience and money system that works fine when I kill a player with default weapons from gmod (such as pistol/crowbar/smg/etc.) but when I use my custom SWEP (magnum) to kill the player I get the following error:


 [ERROR] gamemodes/gamemodename/gamemode/init.lua:55: attempt to call method 'Team' <a nil value>
  1. unknown - gamemodes/gamemodename/gamemode/init.lua:55
     2. FireBullets - [C]:-1
        3. unknown - gamemodes/gamemodename/entities/weapons/weapon_magnum.lua:65

When getting killed by the SWEP (magnum) I get changed to some non-assigned team then respawn and this error shows up:


 [ERROR] gamemodes/gamemodename/gamemode/init.lua:55: attempt to call method 'Team' <a nil value>
  1. unknown - gamemodes/gamemodename/gamemode/init.lua:55


It is my belief that the SWEP messes up what team I am on when I die (as opposed to the default gmod weapons not messing up the teams) but am not sure how to correct this.

Here is the related code for the init.lua:



function GM:PlayerDeath( ply, item, attacker )
	print( "Player: " .. ply:Nick() .. " has died." )
	if( ply:Team() == 4 && attacker:Team() != 4 ) then -- Line 55 (Team 4 is the only team that cannot use the magnum, Team 2 and 3 can use it)
		attacker:StatsAddXP( 50 )
		attacker:StatsAddCoins( 50 )
	elseif ( ply:Team() == 3 && attacker:Team() == 4 ) then
		attacker:StatsAddXP( 10 )
		attacker:StatsAddCoins( 10 )
	elseif ( ply:Team() == 2 && attacker:Team() == 4 ) then
		attacker:StatsAddXP( 10 )
		attacker:StatsAddCoins( 10 )
	end
	ply:SetTeam( 2 )
end


Here is the related code for the magnum SWEP:




function SWEP:PrimaryAttack()
	
	if( not self:CanPrimaryAttack() ) then
		return
	end
	
	local ply = self.Owner

local Bullet = {}
		Bullet.Num		=	self.Primary.NumShots
		Bullet.Src		=	ply:GetShootPos()
		Bullet.Dir		=	ply:GetAimVector()
		Bullet.Spread	= 	Vector( self.Primary.Spread, self.Primary.Spread, 0 )
		Bullet.Tracer	=	0
		Bullet.Damage	=	self.Primary.Damage
		Bullet.AmmoType =	self.Primary.Ammo
		
	self:ShootEffects()
	self:FireBullets( Bullet ) -- Line 65
        .....
end


Maybe the attacker it’s not a player

Check that attacker is a player before calling player-only metafunctions. Attacker can also be an entity, and therefore Team would be a nil method.

[editline]e[/editline]

Damn ninja.

That makes sense. It’s hard for me to test as I need a friend online to help but would attacker:GetOwner():Team() work to output the player team?

Only if the attacker was an entity and that entity had an owner.

wouldn’t just attcker:IsPlayer() be sufficient here?

Just got a friend to help and I put in attacker:GetOwner():Team()

The problem is fixed. The attacker was the SWEP thus the owner must have been a player. Thanks for the help! Much appreciated.

What happens when an npc kills you then?

I’m not sure, NPCs aren’t used in my gamemode. For arguments sake if an NPC did kill a player it would be best the NPC didn’t receive XP and Coins anyway lol. It is likely an error would trigger though so I do appreciate you bringing that up.

Just run the check IsPlayer on the entity, and that will solve the case in one check