self.Owner errors

Very new Lua coder here! :smile:

Anyway just to have a small test of what I can do with Lua. I’m creating a “Multigun” and it’s going to do 3 different things. I’m 2/3 done with it and I’m polishing a suicide type thing. It works fine but I do get errors.

Code :
[lua]
function SWEP:Reload()

if (SERVER) then

	self.Owner:EmitSound( "weapons/mortar/mortar_shell_incomming1.wav" )
	timer.Simple(1.3, function()

		local xplod = ents.Create( "env_explosion" )
		xplod:SetPos( self.Owner:GetPos() ) -- Error line
		xplod:SetOwner( self.Owner )
		xplod:Spawn()
		xplod:SetKeyValue( "iMagnitude", "300" )
		xplod:Fire( "Explode", 0, 0 )
		xplod:EmitSound( table.Random(mortarsounds), 100, 100 )
	end )
end

end
[/lua]

It generates this error :



Timer Error: ...dons\multigun\lua\weapons\weapon_multigun\shared.lua:63: attempt to index field 'Owner' (a nil value)


I’ve already found ways to get past multiple errors before this. (Putting the env_explosion in the timer function instead of calling a whole new function for example) But this has me stumped.

You’re calling self.Owner in a timer, which does not supply the entity self in the timer.

[lua]
function SWEP:Reload()

if (SERVER) then

	self.Owner:EmitSound( "weapons/mortar/mortar_shell_incomming1.wav" )
	timer.Simple(1.3, function(self)

		local xplod = ents.Create( "env_explosion" )
		xplod:SetPos( self.Owner:GetPos() ) -- Error line
		xplod:SetOwner( self.Owner )
		xplod:Spawn()
		xplod:SetKeyValue( "iMagnitude", "300" )
		xplod:Fire( "Explode", 0, 0 )
		xplod:EmitSound( table.Random(mortarsounds), 100, 100 )
	end, self )
end

end
[/lua]
Fixed for you.

I’m gonna guess you meant the way Ralle fixed it? Or that I can’t call self.Owner in a timer?

The fix Ralle posted doesn’t work though. I still get the exact same error.