How to scan through multiple models and find how far away they are

So I am making a keycard interaction system.

It will work by looping through a folder where all scanners are and then finding their distance between you and all of them.

And if it finds a scanner in 5 stud range of you or closer it will make your character go to it but I am not sure how can I make a loop to do this for maybe like 20 scanners.

Each model needs a root because checking for distances can only happen between parts, so that’s the first thing you need to identify across all the models you want to check through. From there, the answer comes from your thread, loop through them and compare distances between the model’s root and your character’s HumanoidRootPart.

Adding on to what he said, to find the distance you should use Magnitude. Then, do a loop checking if the Magnitude is 5 or lower, and teleport them there.

I know how to use magnitude I’m just asking how can I compare each of the parts in the scanner folder, this is what I have currently but works sometimes and sometimes doesn’t

local userInputService = game:GetService("UserInputService")
local allScanners = game.Workspace.Scanners
local Camera = game.Workspace.CurrentCamera


userInputService.InputBegan:Connect(function(inputObject, gameProcessed)
    if gameProcessed then return end
        if inputObject.KeyCode == Enum.KeyCode.E then
		for _, scanner in next, allScanners:GetChildren() do
		if (scanner.Center.Position - game.Players.LocalPlayer.Character.UpperTorso.Position).Magnitude <= 4 then 
			scanner.ScannerEvent:FireServer()
	end
	end
	end
end)