Prop Hunt Player Class Error

Well this happends randomly i don’t know what is causing it…


[ERROR] gamemodes/prop_hunt/gamemode/player_class/class_hunter.lua:51: Tried to use a NULL entity!
1. UnLock - [C]:-1
2. unknown - gamemodes/prop_hunt/gamemode/player_class/class_hunter.lua:51

Timer Failed! [Simple][@gamemodes/prop_hunt/gamemode/player_class/class_hunter.lua (line 60)]

class_hunter.lua:


// Create new class
local CLASS = {}


// Some settings for the class
CLASS.DisplayName			= "Hunter"
CLASS.WalkSpeed 			= 230
CLASS.CrouchedWalkSpeed 	= 0.2
CLASS.RunSpeed				= 230
CLASS.DuckSpeed				= 0.2
CLASS.DrawTeamRing			= false


// Called by spawn and sets loadout
function CLASS:Loadout(pl)
	pl:Give("weapon_crowbar")
	pl:GiveAmmo(64, "Buckshot")
	pl:GiveAmmo(255, "SMG1")
	pl:Give("weapon_shotgun")
	pl:Give("weapon_smg1")

	if GetConVar("WEAPONS_ALLOW_GRENADE"):GetBool() then
		pl:Give("item_ar2_grenade")
	end
	
	local cl_defaultweapon = pl:GetInfo("cl_defaultweapon") 
 	 
 	if pl:HasWeapon(cl_defaultweapon) then 
 		pl:SelectWeapon(cl_defaultweapon)
 	end 
end


// Called when player spawns with this class
function CLASS:OnSpawn(pl)
	local unlock_time = math.Clamp(GetConVar("HUNTER_BLINDLOCK_TIME"):GetInt() - (CurTime() - GetGlobalFloat("RoundStartTime", 0)), 0, GetConVar("HUNTER_BLINDLOCK_TIME"):GetInt())
	
	//function MyLockFunc()
	//function MyUnlockFunc()
	
	local unblindfunc = function()
		//MyUnblindFunc(pl.Blind(false))
		pl:Blind(false)
	end
	local lockfunc = function()
		//MyLockFunc(pl.Lock())
		pl.Lock(pl)
	end
	local unlockfunc = function()
		//MyUnlockFunc(pl.UnLock())
		pl.UnLock(pl)
	end
	
	if unlock_time > 2 then
		pl:Blind(true)
		
		timer.Simple(unlock_time, unblindfunc)
		
		timer.Simple(2, lockfunc)
		timer.Simple(unlock_time, unlockfunc)
	end
end


// Called when a player dies with this class
function CLASS:OnDeath(pl, attacker, dmginfo)
	pl:CreateRagdoll()
	pl:UnLock()
end


// Register
player_class.Register("Hunter", CLASS)

Is there any way to fix that?

It happens when the timer runs and the player has already disconnected.

Add a check to see if the player still exists before running the unlock.

Well i’m a noob in this, can anyone help?

Edit:
It doesn’t matter, server had 18 players when that error came again.

Ugh

Can anyone explain?

http://puu.sh/bu2fN/5835112aca.png

I started with lua like 2 weeks ago and that wouldn’t help much.

People here are like elitists of coding, they will tease you with vague answers but never really give a solution that the common person gets.

I’m on mobile so I dont know the line numbers to help much. But google “gmod timer.simple” and “gmod IsValid”. Have it check if the player is valid before running anything else

They’re right. If you start a timer, you need to verify the data when it runs. There are also some cases where a function may run and take a long time to the point where further down the content is no longer valid ( one of the reasons why I suggest helper-functions, aside from reducing repeating code ).

Add:


if ( !IsValid( _variable ) ) then return; end


in the timer; change _variable to the variable you’re using…

Like this?


if ( !pl:IsValid( timer.Simple ) ) then return; end

Check if the player is valid, not the timer.