I’m starting to code the AI for my game, both friendly NPCs and aggressive NPCs. Both interact with the player as they come near.
Is it better to check on the client if the player is close to an NPC and then fire a remote if they are. Or should I have the server iterate through all players, cache their positions and then do comparisons for each NPC?
local Players = game:GetService("Players")
local NPCs = game:GetService("NonPlayerCharacters")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("HumanoidRootPart")
character.HumanoidRootPart.Touched:Connect(function(part)
if part:IsDescendantOf(NPCs) then
print(player.Name .. " is close to an NPC!")
end
end)
end)
end)