So, the code down below is supposed to check nearby NPCs, every physics update, to see if they are:
- Within the
DETECTION_RANGE
- And to also find out which NPC is the closest to the player
It works as intended, however I can’t help but feel like this is a very inefficient method of doing this. Not only that, but this script seems to create a significant frame drop, when run on slower devices, which is not what I want. Would anyone be able to give suggestions on improving the structure of this script, or anything that will improve the performance of it?
local DETECTION_RANGE = 10
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local NPCFolder = workspace:WaitForChild("DialogueNPCs")
local NPCInRange = nil
local function FindClosestNPC()
local inRangeNPCs = {}
for _, NPC in pairs(NPCFolder:GetChildren()) do
if (char.PrimaryPart.Position - NPC.PrimaryPart.Position).Magnitude <= DETECTION_RANGE then
table.insert(inRangeNPCs, NPC)
end
end
local closestNPC = inRangeNPCs[1]
for i = 2, #inRangeNPCs do
local currentNPC = inRangeNPCs[i]
if (char.PrimaryPart.Position - currentNPC.PrimaryPart.Position).Magnitude < (char.PrimaryPart.Position - closestNPC.PrimaryPart.Position).Magnitude then
closestNPC = currentNPC
end
end
NPCInRange = closestNPC
end
game:GetService("RunService").Heartbeat:Connect(function()
FindClosestNPC()
end)