Help with NPC Relationships

Hello, I’m developing new gamemode that is inspired by Splinter Cell: Chaos Theory. So I am a fairly good Lua coder, but I’m having trouble with NPC Relationships.

hook.Add("Think", "stealthAI", function()
	local light = (render.GetLightColor(LocalPlayer():GetPos())*Vector(255,255,255)):Length()*0.9
	local npc = ents.FindByClass("npc_*")
	local ply = LocalPlayer()
	if light < 3.0 then
		npc:AddEntityRelationship(ply, D_NU, 10)

And I get this error when I walk into the dark.

[ERROR] gamemodes/thekiller/gamemode/cl_init.lua:71: attempt to call method 'AddEntityRelationship' (a nil value)
  1. v - gamemodes/thekiller/gamemode/cl_init.lua:71
   2. unknown - lua/includes/modules/hook.lua:84

Does anyone know a fix for this?

AddEntityRelationship is a serverside function.

Is your script shared/clientsided?

Edit: Nevermind, it is clientsided. You used LocalPlayer().

I think if you did a ‘for’ loop through player.GetHumans() then set the relationship then it’d work serverside with no networking needed. (Probably don’t use player.GetAll() since it would check bots as well)

I’ll try that. Thanks for the help!

[editline]3rd November 2015[/editline]

Nope, it didn’t work. I believe that it should have worked.

hook.Add("Think", "stealthAI", function()
	for k,v in pairs(player.GetHumans()) do
	local light = (render.GetLightColor(v:GetPos())*Vector(255,255,255)):Length()
	local npc = ents.FindByClass("npc_combine_s")
		if light > 3.0 then
			npc:AddEntityRelationship(ply, D_NU, 99)

Sorry, should’ve tested that myself. Don’t know what the problem is though since I’m just a cardboard box

Doesn’t ents.FindByClass return a table? You cant run AddEntityRelationship on a table.
Example courtesy to the wiki

[lua]function ENT:Think()
local enemy = ents.FindByClass( “npc_*” ) --Find any spawned entity in map with class beginning at npc
for _, x in pairs( enemy ) do --for every found entity do
if !x:IsNPC() then return end – if found entity is not NPC then do nothing
if x:GetClass() != self:GetClass() then – if found entity is not self entity then continue
x:AddEntityRelationship( self, D_HT, 99 ) – found entity will hate self entity
self:AddEntityRelationship( x, D_HT, 99 ) – self entity will hate found entity

You also used for k,v with player.GetHumans, but defined the current player value as “ply” instead of “v”.

Also what above said, ents.Find returns a table.

Oh, I’m so stupid. Thanks for telling me! I’ll fix that and see if that works.

Hmm… It seems the relationship function doesn’t seem to work. I’ve asked my friend about this and he said, “All of the default Half Life 2 enemies have hard coded relationships.” Has anyone got the relationship function to work?

Can you show current code?

Instead of looping through all players, why not instead use ents.FindInSphere() based on NPC’s location.

I’m changing my gamemode. I will not need this. I am tagging this as solved.