LookupAttachment - [C]:-1 (Popcorn)

So sometimes when people use the popcorn swep on my server to eat popcorn, other people get this error :



[ERROR] lua/weapons/weapon_popcorn/cl_init.lua:39: Tried to use a NULL entity!
  1. LookupAttachment - [C]:-1
   2. func - lua/weapons/weapon_popcorn/cl_init.lua:39
    3. unknown - lua/includes/modules/net.lua:31


It mostly happens in the first seconds when you just joined the server.
The swep works fine though, the eating works and it does what it’s supposed to do. I’d really like to fix the occasional script error though. This is where I need some help.

Codes touched by the error :
shared.lua



function SWEP:PrimaryAttack()
	if SERVER then
        self.Owner:EmitSound( "crisps/eat.wav", 60)
		self.Owner.BiteStart = 0
		self.Owner.BitesRem = 3
		net.Start("Popcorn_Eat_Start")
			net.WriteEntity(self.Owner)
		net.Broadcast()
	end
	self.Owner.ChewScale = 1
	self.Owner.ChewStart = CurTime()
	self.Owner.ChewDur = SoundDuration("crisps/eat.wav")
	self.Weapon:SetNextPrimaryFire(CurTime() + 12 )
end


cl_init.lua



net.Receive("Popcorn_Eat",function () 
	local ply = net.ReadEntity()
	local size = net.ReadFloat()
	local attachid = ply:LookupAttachment("eyes")
	local emitter = ParticleEmitter(ply:GetPos())
	
	local angpos = ply:GetAttachment(attachid)
	local fwd
	local pos
	
	if (ply != LocalPlayer()) then
		fwd = (angpos.Ang:Forward()-angpos.Ang:Up()):GetNormalized()
		pos = angpos.Pos + fwd*3
	else
		fwd = ply:GetAimVector():GetNormalized()
		pos = ply:GetShootPos() + gui.ScreenToVector( ScrW()/2, ScrH()/4*3 )*10
	end
	
	for i = 1,size do
		if !ply then return end
		
		local particle = emitter:Add( "particle/popcorn-kernel", pos )
		if particle then
			local dir = VectorRand():GetNormalized()
			kernel_init(particle, ((fwd)+dir):GetNormalized() * math.Rand(0,40))
		end
	end
	emitter:Finish()
end)


Thanks in advance!

You didn’t post the lines where the error is occurring; you pasted 30 lines for cl init, and it’s on the 39th line…

But; the problem could be with net.ReadEntity. I’d recommend changing that line, and instead of sending an entity, send an EntIndex. Then, in the place of ply = net.ReadEntity do ply = Entity( net.ReadXXX( ) );

That’s exactly the same as using net.WriteEntity/net.ReadEntity.

The issue is when the networked player is NULL for the client. Simply add an IsValid if-statement after reading the entity in your net.Receive callback.

Thanks. I’ll try this right now!

I agree with you, to a certain extent. If he does it that way, and the player is NULL, and he doesn’t do a check to re-send the message, then he’s up the creek if it’s an important message.

If he uses EntIndex, it never fails, in context when WriteEntity does fail, unless someone hard-codes an ent-index and it doesn’t exist.

If he adds a timer simple 0 around the net.send using WriteEntity, it should also work.

Try it, in some cases the WriteEntity will fail, even though the Entity exists on the server, it doesn’t exist on the client yet, but if you use the EntIndex, and then access via Entity on the client, for some reason it works. It’s odd; but it’s a known bug ( Users sending data too early, or something… ).

https://github.com/garrynewman/garrysmod/blob/master/garrysmod/lua/includes/modules/net.lua#L38-L55

Interesting, Garry either changed it; or the bug is with that code. I’ll do some testing to see if the bug still exists; but it was early in 13 that I started using the WriteLong, ReadLong wrapped by Entity.

If it still exists, then integers must be getting corrupted en route.