"self.Owner:Kill();" gives error "attempt to call method 'Kill' (a nil value) "

I am trying to create a gun that kills the user upon firing, most of the stuff I want works except the kill part

The code section in question:



function SWEP:PrimaryAttack()
    if ( !self:CanPrimaryAttack() ) then return end
    self:TakePrimaryAmmo( 1 )
    self.Weapon:EmitSound( "Weapon_Pistoĺ.Single" )
    self:ShootBullet( 0, 1, 0.01 )
    self.Owner:ViewPunch( Angle( -1, 0, 0 ) )
    if SERVER then 
        self.Owner:StripWeapon("weapon_ttt_test")
        self.Owner:Kill();
     end
end


and the error:



[ERROR] addons/testmodpleaseignore/gamemodes/terrortown/entities/weapons/weapon_ttt_test.lua:60: attempt to call method 'Kill' (a nil value) 
  1. unknown - addons/testmodpleaseignore/gamemodes/terrortown/entities/weapons/weapon_ttt_test.lua:60
 

EDIT: Solved it



function SWEP:PrimaryAttack()
	var = self.Owner
	print(var)
	if ( !self:CanPrimaryAttack() ) then return end
	self:TakePrimaryAmmo( 1 )
	self.Weapon:EmitSound( "Weapon_Pistoĺ.Single" )
	self:ShootBullet( 0, 1, 0.01 )
	self.Owner:ViewPunch( Angle( -1, 0, 0 ) )
	if SERVER then 
		var:StripWeapon("weapon_ttt_test")
		var:Kill();
 	end
end


Please make var local. There is no reason that should be global.

Why not? inefficient allocation of memory is the best way to go!

(sarcasm)

How would I do that? I am started using lua like 15 minutes before that post so I have no idea about lua.

Programming in Lua; Local Variables and Blocks

:snip: sorry that was rude

I figured if you guys complain about it that you would atleast tell me how to do it right, guess I was wrong.

Just place “local” behind it like so:


local var = self.Owner

If you’re not going to access a var outside of a certain code block/function there’s no reason to make it global.

You need to be willing to research and look at other existing code in order to figure things out. You can’t expect everyone to always have the time or energy to feed you the code.

Sorry for being rude before, it’s just that it seems bad to me to clog the thread further after only 15 minutes learning Lua, when there’s even more people with hours of experience needing help.

:snip: I never read do I

This bit still applies:

‘Var’ isn’t a very descriptive variable name. Usually if you use variables, you should name them something that you can understand just by reading the name. In this case, you could name it ‘ply’, short for ‘player’, so you know the type of the variable when referencing it later.

The reason why you’re getting the error is because you’re removing the weapon, and therefore making self nil. Your fix is an easy fix, but less code can be used by instead using


function SWEP:PrimaryAttack()
    if ( !self:CanPrimaryAttack() ) then return end
    self:TakePrimaryAmmo( 1 )
    self:EmitSound( "Weapon_Pistoĺ.Single" )
    self:ShootBullet( 0, 1, 0.01 )
    self.Owner:ViewPunch( Angle( -1, 0, 0 ) )
    if SERVER then
        self.Owner:Kill();
        self:Remove()
     end
end


This is literally the original snippet that gave him trouble. The cause of the problem is that StripWeapon makes the weapon no longer belong to the player, thereby making self.Owner equal NULL. Using a local variable to save self.Owner in such a way that it can’t change is the right way to do it.

Novian30002, your “solved” code is good. Only thing you should do is give var a better name and make it local:

[lua]function SWEP:PrimaryAttack()
local owner = self.Owner
–print(owner)
if ( !self:CanPrimaryAttack() ) then return end
self:TakePrimaryAmmo( 1 )
self.Weapon:EmitSound( “Weapon_Pistoĺ.Single” )
self:ShootBullet( 0, 1, 0.01 )
owner:ViewPunch( Angle( -1, 0, 0 ) )
if SERVER then
owner:StripWeapon(“weapon_ttt_test”)
owner:Kill();
end
end[/lua]

2

He updated his post. Literally all it takes is swapping two lines around (by the very same reason you put in your post, due to self.Owner becoming NULL). No need to extra variables, just got to learn the right way of doing things. Creating the owner variable is a cheap work around to an easy problem to fix. And I think there is more to learn from this error than “oh it’s a nil value, better save it” - it should inspire the thought of “Maybe the ordering of things does matter”.

Why bother stripping the weapon if you kill the player right away?

What Gmod4phun said plus once you strip a weapon it no longer has an owner. If you kill the owner before stripping it it should work fine.