local U = require "scriptbank\\utillib" local lightNum = {} local attachTo = {} attachLightToMe = {} function light_npc_init(e) lightNum[e] = GetEntityLightNumber(e)--change 2 to the entity number of the light --change 'nil' to the entity number of the npc --or place the light next to entity for automatic assignment --*make sure entity has 'attachLightToMe[e] = e' in the script* attachTo[e] = nil end function light_npc_main(e) if attachTo[e] == nil then local x1,y1,z1 = GetLightPosition(lightNum[e]) local dist1 = 999999 for a,_ in pairs (attachLightToMe) do local x2,y2,z2 = GetEntityPosAng(a) local dist2 = sqrd(x1,z1,x2,z2) if dist2 < dist1 then attachTo[e] = a dist1 = dist2 end end if attachTo[e] == nil then attachTo[e] = -1 end else if attachTo[e] == -1 then return end local x, y, z, Ax, Ay, Az = GetEntityPosAng(attachTo[e]) SetLightPosition ( lightNum[e], x, y+250, z ) end end function sqrd( x1, z1, x2, z2 ) local dx, dz = x1 - x2, z1 - z2 return dx*dx+dz*dz end