Attaching camera to player's eyes

I am making a very little and (sometimes) usefull addon, which makes player’s camera be attached to playermodel’s eyes. (My stupid english)
But when i run GMod, nothing happens. When i die, console gives me error:

Here’s my code:


if( SERVER ) then
  
     AddCSLuaFile( "autorun/player_eyes.lua" );
  
end

if( CLIENT ) then
  
     local function CalcView( pl, origin, angles, fov )
   
       local player = Ent:IsPlayer();
       if( !player || player == NULL || !pl:IsValid() ) then return; end
       
        local eyes = pl:GetAttachment( pl:LookupAttachment( "eyes" ) );
        
         local view = {
             origin = eyes.Pos,
             angles = eyes.Ang,
			 fov = 90, 
         };
        
         return view;
     
      end
      hook.Add( "CalcView", "PlayerEyes", CalcView );
    
  end 



What’s wrong?

P.S. I am new to Lua :slight_smile:

You can’t whip variables out of nowhere (Ent is null)

[lua]if( SERVER ) then
AddCSLuaFile( “autorun/player_eyes.lua” );
else
local function CalcView( pl, origin, angles, fov )
if not ValidEntity(pl) or not pl:IsPlayer() then return end

	local eyes = pl:GetAttachment( pl:LookupAttachment( "eyes" ) );

	local view = {
		origin = eyes.Pos,
		angles = eyes.Ang,
		fov = 90, 
	};

	return view;

end
hook.Add( "CalcView", "PlayerEyes", CalcView );

end[/lua]

Hmmm… I’ll try it now and i’ll say you result.

[editline]09:21PM[/editline]

It works! But only when I’m dying …