self.Owner coming up nil?

Hello, I’m garbage when it comes to code but I would like some help please. I have a custom weapon base and that, on some random occasions, will spam the console with the bug below when picking up a gun (in TTT). It can be fixed by dropping and picking up the weapon but still, it’s a bug. Your gun also becomes invisible when this error occurs.

I believe it is because self.Owner comes up nil and I am not sure how exactly I would fix it. Here are the bug(s), thanks :slight_smile:

[ERROR] addons/sleek ttt base/lua/weapons/swb_base/cl_model.lua:198: attempt to call method 'GetViewModel' (a nil value)
1. unknown - addons/sleek ttt base/lua/weapons/swb_base/cl_model.lua:198

[ERROR] addons/sleek ttt base/lua/weapons/swb_base/cl_hud.lua:89: attempt to call method 'ShouldDrawLocalPlayer' (a nil value)
1. unknown - addons/sleek ttt base/lua/weapons/swb_base/cl_hud.lua:89

The line of code in cl_model at line 198:
vm = self.Owner:GetViewModel()

And here is line 89 of cl_hud
lp = self.Owner:ShouldDrawLocalPlayer()

I should also mention that it doesn’t happen mid round, usually right after they spawn. Possibly suggesting it is because they haven’t finished loading yet?
Thanks a ton in advance!

Does someone happen to be holding the weapon with a magneto stick when the player picked it up?

Put IsValid( self.Owner ) checks.

My concern is why self.Owner is nil in the first place. It could be because of a TTT bug I’ve already submitted a fix for.

Because when the weapon is dropped it has no owner?

[editline]4th May 2014[/editline]

Also, ShouldDrawLocalPlayer is clientside only, so the chances are you are trying to call in on server.

[editline]4th May 2014[/editline]

And GetViewModel only works for LocalPlayer afaik.

I know but he says the problem appears when it is picked up. Not all the time but sometimes.

The self.Owner isn’t yet set as soon as the weapon is picked up?

in either case, IsValid(self.Owner ) must be there.

Depends what hooks those two lines are in. The fact it that it only happens sometimes and that it will only fix itself when it’s dropped and picked up again makes me think it’s the owner not being set at all.

Are you using the sleek weapon base from CoderHire?
Why not ask the creator of the addon if so?

Check if the owner is valid
[lua] if IsValid( self.Owner ) then … [/lua]

I’ll look into that Magneto Stick fix you put and add a IsValid check. Thanks :slight_smile:

[editline]4th May 2014[/editline]

I am using the Sleek Weapon Base but the creator is clueless when it comes to TTT.