Best way to create enemy NPC target detection?

I’ve previously tried methods such as looping through all players and comparing magnitude, I’m sure you can imagine how bad that was, I’ve thought of creating a region3 around the NPC and checking for players inside that, but I’m unsure as to how demanding this would be in a game with 20, 30 players all fighting, etc. Any help is appreciated.

so your trying to check if a player is close enough to an NPC?

If you’re so worried about this issue then try creating a “chunk” system where each NPC can only see the players in its current chunk. Personally I don’t think you will have an issue with comparing the distance from players.

Perhaps create a “Chunk” system and have each enemy request to see who is in their own chunk. Should not be expensive.

You can define a player’s current chunk by taking their current position and dividing it by the chunk size and rounding it down.

Under the assumption you set your chunks up in an Chunks[X][Y][Z] fashion:

local Position = ...
local ChunkSize = 16

local X = math.floor(Position.X / ChunkSize) * ChunkSize 
local Y = math.floor(Position.X / ChunkSize) * ChunkSize 
local Z = math.floor(Position.X / ChunkSize) * ChunkSize 

Or, perhaps a more suspicious method…

Giant parts around each NPC with .Touched signals!