Tried to use a NULL entity! 1. SetBodygroup - [C]:-1

My server launched today and we’ve been getting some issues with the SWEPs that I cooked up for it. An error is spammed constantly at random intervals by random SWEPs on the server, even if they aren’t being held by any players. The odd thing is, you’d think if it returns an error for the code not to work, right…? Well, no, that’s not the case here, as this weapon base does exactly what I expect of it - the weapon’s bodygroups are all set to 0 when holstered and they’re set to whatever they’re designated to be set to when the weapon is active. This is what’s really confusing me.

This happens with basically any SWEP that uses bodygroups, which is most of the weapons on my server.

The error:

[ERROR] addons/nutscript 1.1/lua/weapons/nut_sks.lua:21: Tried to use a NULL entity!
  1. SetBodygroup - [C]:-1   
  2. unknown - addons/nutscript 1.1/lua/weapons/nut_sks.lua:21

The code for the weapon that just so happened to be causing the error that time:

if (CLIENT) then

	SWEP.PrintName			= "SKS"	
	SWEP.Author				= "Tazmily"

	SWEP.SlotPos			= 3
	SWEP.IconLetter			= "i"
	SWEP.NameOfSWEP		= "nut_SKS" --always make this the name of the folder the SWEP is in. 
	killicon.AddFont( SWEP.NameOfSWEP, "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )

function SWEP:Deploy()
	self.Owner:GetViewModel():SetBodygroup(1, 0)
		self:SetBodygroup(1, 0)
      return true
function SWEP:Holster()
	self.Owner:GetViewModel():SetBodygroup(1, 0)
		self:SetBodygroup(1, 0)
      return true
function SWEP:Initialize()

SWEP.Category				= "Nutscript 1.1"
SWEP.Base					= "rcs_base"

SWEP.Spawnable				= true
SWEP.AdminSpawnable			= true

SWEP.ViewModel				= "models/tnb/weapons/c_sks.mdl"
SWEP.WorldModel				= "models/tnb/weapons/w_sks.mdl"

SWEP.Weight					= 5
SWEP.AutoSwitchTo			= false
SWEP.AutoSwitchFrom			= false

SWEP.Primary.Sound			= Sound( "weapons/m249/m249_fire.wav" )
SWEP.Primary.Recoil			= 0.1
SWEP.Primary.Damage			= 45
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.001
SWEP.Primary.ClipSize		= 10
SWEP.Primary.Delay			= .20
SWEP.Primary.DefaultClip	= 0
SWEP.Primary.MaxReserve		= 90
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "smg1"
SWEP.UseHands = true
SWEP.ViewModelFlip = false
SWEP.ViewModelFOV = 55

SWEP.Primary.MaxSpread		= 0.000001 --the maximum amount the spread can go by, best left at 0.20 or lower
SWEP.Primary.Handle			= 0.2 --how many seconds you have to wait between each shot before the spread is at its best
SWEP.Primary.SpreadIncrease	= 0.21/15 --how much you add to the cone after each shot

SWEP.MoveSpread				= 6 --multiplier for spread when you are moving
SWEP.JumpSpread				= 8 --multiplier for spread when you are jumping
SWEP.CrouchSpread			= 0.5 --multiplier for spread when you are crouching

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo			= "none"

SWEP.Zoom					= 0 --pretty self explanitory, don't change this unless if you want the gun to be fucked up
//SWEP.ZoomOutDelay			= 0.2 -- this is used for the delay between when you shoot and when it zooms out to pull the bolt
//SWEP.ZoomInDelay			= 1.5 --always set this 0.2 higher than SWEP.Primary.Delay
SWEP.Zoom1					= 30 --Field of view for the first zoom
SWEP.Zoom2					= 15 --field of view for the second zoom

SWEP.Zoom0Cone				= 0.1 --spread for when not zoomed
SWEP.Zoom1Cone				= 0.01 --spread for when zoomed once
SWEP.Zoom2Cone				= 0.01 --spread for when zoomed twice

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo			= "none"

SWEP.IronSightsPos = Vector( -6.3496, 0, 4.247 )
SWEP.IronSightsAng = Vector( 0.3449, 0.1386, 0 )

I’m using a very, very lightly modified RCS base. The only modifications I made were to make it iron sight upon hitting right click and to remove the iron sights’ cross hair. This does very little to the functionality of the weapon, as the SWEPs themselves still work just fine - it’s just really annoying for our players and in-server admins, and it makes reading rcon logs hard due to the spam. Any suggestions?

self.Owner:GetViewModel() only exists clientside.

Shit. Any function that works in a server context I can use in its place? (not variable wow it’s late)

edit: Could I use Player:GetViewModel() in its place?

You don’t need to change the view model on the server, it only exists on client and you only need to apply changes to it on client.

[sp]Add “if CLIENT then end” to your code[/sp]

That was actually my first thought actually but when I saw the Player:GetViewModel() on the wiki I figured it’d be worth a shot. I’ll check it out whenever I get the chance to tomorrow.

Can you give a little bit of elaboration on where exactly to put it, and what to put between the then and end? I pasted just that in the top of the code but it didn’t seem to actually do anything, as the errors still persisted.

[sp]I’m kind of dumb sorry[/sp]