Lua Errors form file matproxy/playercolor

[lua]
[ERROR] lua/matproxy/player_color.lua:28: Tried to use a NULL entity!

  1. GetPlayerColor - [C]:-1
  2. bind - lua/matproxy/player_color.lua:28
    3. unknown - lua/includes/modules/matproxy.lua:56

[/lua]

This lua errors I get randomly in Gamemode: Sandbox if I delete the file i dont get them but the file is important for the player-color youre player has got
Anyone has Ideas why this appears?

What…
Give code perhaps?

At which point do you get the error?

I get the errors spammed in my console if players get banned or something went wrong with the player spawning.

This is a code that could cause the error dont know if it does.
[lua]
– Kleiner Model
function Kleiner(ply)
ply:SetModel( “models/player/kleiner.mdl” )
end

hook.Add(“PlayerSpawn”, “SetModel_Kleiner”, Kleiner)
[/lua]

The code is made because I want players to buy skins from pointshop and it works because pointshop sets the model 1 second after the kleiner model was set :smiley:

as a rule of thumb, always have error checking. check the validity of the player just so that if for some strange reason (which, there are plenty of in gmod) the player is invalid, the code won’t run and you won’t see errors.

Can you paste the script/version you have? I remember I changed something locally in that file at one point because I got an error too.

You mean the matproxy/player file?
Matpoxy:
[lua]
–[[---------------------------------------------------------
PlayerColor Material Proxy
Sets the clothing colour of custom made models to
ent.GetPlayerColor, a normalized vector colour.
-----------------------------------------------------------]]

matproxy.Add( {
name = “PlayerColor”,

init = function( self, mat, values )
	-- Store the name of the variable we want to set
	self.ResultTo = values.resultvar
end,

bind = function( self, mat, ent )
	if not IsValid( ent ) then return end

	-- If entity is a ragdoll try to convert it into the player
	-- ( this applies to their corpses )
	if ( ent:IsRagdoll() ) then
		local owner = ent:GetRagdollOwner()
		ent = IsValid(owner) and owner or ent
	end

	-- If the target ent has a function called GetPlayerColor then use that
	-- The function SHOULD return a Vector with the chosen player's colour.
	if ( ent.GetPlayerColor ) then
		local col = ent:GetPlayerColor()
		if ( isvector( col ) ) then
			mat:SetVector( self.ResultTo, col )
		end
	else
		mat:SetVector( self.ResultTo, Vector( 62.0/255.0, 88.0/255.0, 106.0/255.0 ) )
	end
end 

} )
[/lua]

Ok fixed it don’t know why this cause errors but I only changed
[lua]
– Kleiner Model
function Kleiner(ply)
ply:SetModel( “models/player/kleiner.mdl” )
end

hook.Add(“PlayerSpawn”, “SetModel_Kleiner”, Kleiner)
[/lua]
to that
[lua]
– Kleiner Model
function Kleiner(ply)
timer.Simple(0.1, function() ply:SetModel( “models/player/kleiner.mdl” ) end)
end

hook.Add(“PlayerSpawn”, “SetModel_Kleiner”, Kleiner)
[/lua]

I really didnt understand why that was the problem…