SWEP Effect: Errors when owner dies while FIRING

EDIT: BY THE WAY, I forgot to mention that this bug ONLY happens in MULTIPLAYER.
SINGLEPLAYER does not have this bug.

Hello,

I am currently working on a laser beam SWEP where when primary fire is held down, a constant laser beam is emitted.

The laser beam is comprised of numerous custom effects initialized elsewhere/ (No problem finding them or anything like that, weapon works just fine when fired)

I have run into a nasty kind of problem I cannot seem to solve, if the owner of the weapon dies while firing it and emitting the effects. The console will be flooded with numerous and repeated errors.

Which all seem to source from the owner of the weapon no longer existing, and since the effects depend on them, it throws up errors. Yet I have tried many different solutions, If statements checking if the owner is no longer valid/alive/holding the weapon. If the weapon is valid… etc. None have seemed to fix this and I have run out of ideas.

Here is the SWEP’s code concerning ONE of the effects (The others have been commented for the sake of focusing on the issue. They act similarly anyways)

image

Here is the function for the effects declared inside of the SWEP file.

image

And here is the effect file itself.

If there is something I am doing abhorrently wrong, please let me know because I am at a total loss as of what to do at this point.

Thank you for your time.
-Azbr.

Are you able the send all the code for the effect lua file?

Yes.

[lua]

local laser1 = Material(“effects/blueblacklargebeam”)
local laser2 = Material( “effects/bluelaser2” )

function EFFECT:Init(data)
if data:GetEntity():IsValid() then
self.Start = data:GetStart()
self.Weapon = data:GetEntity()
self.End = self.Weapon:GetOwner():GetEyeTrace().HitPos
self.Attach = data:GetAttachment()
self.StartPos = self:GetTracerShootPos(self.Start, self.Weapon, self.Attach) + self.Weapon:GetOwner():EyeAngles():Forward() * 13
self:SetRenderBoundsWS(self.Start, self.End)
end
end

function EFFECT:Render()
if self.StartPos then
render.SetMaterial(laser1)
render.DrawBeam(self.StartPos, self.End, math.sin(CurTime()*15)*10, 0, 20, Color(0, 50, 220, 255))
render.SetMaterial(laser2)
render.DrawBeam(self.StartPos, self.End, math.sin(CurTime()*20)*5, 0, 20, Color(0, 50, 220, 255))
end
end

[/lua]
(I can never get this part to work, here’s what it’s supposed to look like)

I did actually find a fix for this, I added IF statements to both functions to check if data:GetEntity() and self.StartPos existed, so in the case where either of them do not: this will not try to execute the code.

Can you post this on Pastebin or on Gist?

That code is different from the error line would be:

self.End = self.Weapon:GetOwner():GetEyeTrace().HitPos

Try this instead:

if( not IsValid( self.Weapon ) or not IsValid( self.Weapon:GetOwner() ) ) then return end
self.End = self.Weapon:GetOwner():GetEyeTrace().HitPos

Here you go, I think I did this correctly.

In relation to the possible fix given by @Brickwall, here is what I would have changed in the first block:

function EFFECT:Init(data)
    local weapon = data:GetEntity()

    if weapon:IsValid() and weapon:IsWeapon() then
        local owner = weapon:GetOwner()

        self.Start = data:GetStart()
        self.Weapon = weapon
        self.End = owner:IsValid() and owner:GetEyeTrace().HitPos or self.Start
        self.Attach = data:GetAttachment()
        self.StartPos = self:GetTracerShootPos(self.Start, self.Weapon, self.Attach) + self.Weapon:GetOwner():EyeAngles():Forward() * 13
        self:SetRenderBoundsWS(self.Start, self.End)
    end
end

And in the second block:

function EFFECT:Render()
    if self.StartPos and self.End then
        render.SetMaterial(laser1)
        render.DrawBeam(self.StartPos, self.End, math.sin(CurTime()*15)*10, 0, 20, Color(0, 50, 220, 255))
        render.SetMaterial(laser2)
        render.DrawBeam(self.StartPos, self.End, math.sin(CurTime()*20)*5, 0, 20, Color(0, 50, 220, 255))
    end
end

Technically, it should no longer have this kind of error when the owner of the weapon is invalid due to X or Y reasons. However, it seems strange to me that the player becomes NULL when he dies (I can’t test this at the moment) but I think the error comes from the tracing just becoming invalid when the player is simply dead.

Yeah thats better, only thing to change is using IsValid() instead of ent:IsValid()

data:GetEntity() returns NULL or nil on failure?

Thanks guys, I’ll set this up and let you know how it turns out,
As for what’s becoming null, I figured it was the weapon becoming null and then the effect not having an attachment /origin point to latch on to anymore.

Yeah, that sounds right.