How can I prioritise one object over another using raycasts?

I have a script for an NPC that, for every heartbeat, loops through all the NPC in a certain folder and checks if they are within a certain range. But I can’t think of how I can prioritize the closest NPC. I assume I’d use a table to store all the NPCs in range, but I don’t know how I can keep the for loop from putting the same NPC into the array over and over.

Edit:
I figured out how to prioritize one over another, but now my issue is actually updating the closestDistance.

Here is my current code: (Updated)

local runService = game:GetService("RunService")
local soldiersFolder = workspace:WaitForChild("Soldiers")

local currentSoldier = script.Parent
local soldeirsInRange = {}
local closestDistance = math.huge
local closestSoldier = nil

runService.Heartbeat:Connect(function()
	for i, Soldier in soldiersFolder:GetChildren() do
		if Soldier ~= currentSoldier then
			local newRay = Ray.new(currentSoldier.PrimaryPart.Position, Soldier.PrimaryPart.Position)
			local distance = (currentSoldier.PrimaryPart.Position - Soldier.PrimaryPart.Position).magnitude
			
			print(distance)
			
			if distance <= 10 then
				if not table.find(soldeirsInRange, Soldier) then
					table.insert(soldeirsInRange, Soldier)
				end
				
				if distance < closestDistance then
					closestDistance = distance
					closestSoldier = Soldier
				end
				
				currentSoldier.PrimaryPart.CFrame = CFrame.lookAt(currentSoldier.PrimaryPart.Position, closestSoldier.PrimaryPart.Position)
			end
		end
	end
end)
Old Code
local runService = game:GetService("RunService")
local soldiersFrame = workspace:WaitForChild("Soldiers")
local currentSoldier = script.Parent

runService.Heartbeat:Connect(function()
	for i, Soldier in soldiersFrame:GetChildren() do
		if Soldier ~= currentSoldier then
			local newRay = Ray.new(currentSoldier.PrimaryPart.Position, Soldier.PrimaryPart.Position)
			local distance = (currentSoldier.PrimaryPart.Position - Soldier.PrimaryPart.Position).magnitude
			
			print(distance)
			
			if distance <= 10 then
				print("In Range!")
			end
		end
	end
end)

Thanks in advance to any replies!