Lamp doesn't parent to head

I decided to just try attaching an flashlight to an NPC myself. I decided to use the NPC camera code since I don’t know how to use LUA, i’ll ask for permission later since I haven’t released it. I tried parenting an env_projectedtexture to the head but it isn’t parented, it follows the general position of the NPC but it doesn’t parent the env_projectedtexture to the head or more specificly eyes. Also how would I parent an point_spotlight with the env_projectedtexture as well? It also doesn’t cast dynamic shadows which I had for in mind. I also can’t parent it to the hand or more specifically anim_attachment_LH. Help?
TOOL.Category = “Render”
TOOL.Name = “#NPCVision
TOOL.Command = nil
TOOL.ConfigName = “”

TOOL.ClientConVar[ “key” ] = “0”
TOOL.ClientConVar[ “toggle” ] = “1”

GAMEMODE.CameraList = GAMEMODE.CameraList or {}

if ( CLIENT ) then
language.Add( “NPCVision”, “NPC Camera” )
language.Add( “Tool_npcvision_name”, “NPC Camera Tool” )
language.Add( “Tool_npcvision_desc”, “Creates a camera inside of a NPC’s head.” )
language.Add( “Tool_npcvision_0”, “Left click on a NPC to create a camera. Right click to remove it from the NPC’s head.” )
language.Add( “Tool_npcvision_key”, “Key:” )
language.Add( “Tool_npcvision_toggle”, “Toggle:” )
language.Add( “Undone_NPC Camera”, “Undone NPC Camera” )

function TOOL:LeftClick( trace )

local key    = self:GetClientNumber( "key" )
if (key == -1) then return false end

if ( !trace.Entity )            then return end
if ( !trace.Entity:IsValid( ) ) then return end
if ( !trace.Entity:IsNPC( ) )   then return end

if ( CLIENT ) then return true end

local ply = self:GetOwner()
local pid = ply:UniqueID()
local toggle = self:GetClientNumber( "toggle" )
GAMEMODE.CameraList[ pid ]     = GAMEMODE.CameraList[ pid ] or {}
local CameraList = GAMEMODE.CameraList[ pid ]

local attname = "eyes"

local head = trace.Entity:LookupAttachment( "eyes" )
if ( head == 0 ) then
    attname = "Eye"
    head = trace.Entity:LookupAttachment( "Eye" )
    if ( head == 0 ) then -- No eyes :(

-- Already have a camera ? Remove it
local cam
cam = trace.Entity.NPC_ViewCam

if ( cam && cam:IsValid() ) then

-- If the camera already exists then remove it
if (CameraList[ key ] && CameraList[ key ] != NULL ) then
    local ent = CameraList[ key ]

-- Create the camera
local cam = ents.Create( "env_projectedtexture" )
cam:SetParent( trace.Entity )

-- Position and rotate the camera correctly
local attachment = trace.Entity:GetAttachment( head )

local ang = attachment.Ang
local pos = attachment.Pos
cam:SetPos( pos )
cam:SetAngles( ang )

cam:Spawn( )

cam:SetNotSolid( true )
cam:GetTable():SetKey( key )
cam:GetTable():SetPlayer( ply )
cam:GetTable().toggle = toggle

cam:GetTable():SetTracking( NULL, Vector(0) )
if toggle == 1 then
    numpad.OnDown(    ply, key, "Camera_Toggle",  cam )
    numpad.OnDown(    ply, key, "Camera_On",  cam )
    numpad.OnUp(    ply, key, "Camera_Off", cam )

undo.Create("NPC Camera")
    undo.AddEntity( cam )
    undo.SetPlayer( ply )

ply:AddCleanup( "cameras", cam )

CameraList[ key ] = cam

-- Attach it to the head so it moves and rotate with it
cam:Fire( "setparentattachmentmaintainoffset" , attname , 0 )

trace.Entity.NPC_ViewCam = cam

return true


function TOOL:RightClick( trace )

if ( !trace.Entity )            then return end
if ( !trace.Entity:IsValid( ) ) then return end
if ( !trace.Entity:IsNPC( ) )   then return end

if ( CLIENT ) then return true end

if ( trace.Entity.NPC_ViewCam and trace.Entity.NPC_ViewCam:IsValid() ) then
    trace.Entity.NPC_ViewCam = nil
    return true


function TOOL:Think()


function TOOL.BuildCPanel(panel)
panel:AddControl(“Header”, { Text = “#Tool_npcvision_name”, Description = “#Tool_npcvision_desc” })

panel:AddControl("Numpad", { Label = "#Tool_npcvision_key", Command = "npcvision_key", Buttonsize = "22" } )
panel:AddControl("CheckBox", {Label = "#Tool_npcvision_toggle",Command = "npcvision_toggle"})


pos,ang = npc:GetBonePosition(“ValveBiped.Bip01_L_Hand”);

Could use that to get it on the left hand.

So where would I copy paste that?

I made it overwrite:
local ang = attachment.Ang
local pos = attachment.Pos
but it comes up

weapons\gmod_tool\stools/npcvision.lua:70: attempt to index global ‘npc’ (a nil value)

Well you should set the lamp’s position and angle before you parent the lamp. You might also want to parent it after its spawned to.

Is there any good examples of parenting stuff to NPCs?

replace npc with tr.Entity:

and use pos/ang, you may need to do it in some sort of think/draw hook to make sure it stays updated with the client.

Still comes up with attempt to index nil value