Find closest specific object to player?

Hey,

I wanted to know how to find the closest SPECIFIC object to a player.

So I have this script:

while true do
	wait()
	local WorkspaceChild = workspace:GetDescendants()
	for i=1, #WorkspaceChild do 
		local part = WorkspaceChild[i] 
		local Character = script.Parent
		if part:IsA("Model") and part.Name=="DoorSystem" and part:FindFirstChild("Card")  and part:FindFirstChild("DoorSystem1")  and part:FindFirstChild("Close") then
			local torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("UpperTorso")
			local side1 = part:FindFirstChild("Card").side1
			local side2 = part:FindFirstChild("Card").side2
			local magnitude1 = (torso.Position - side1.Position).magnitude
			local magnitude2 = (torso.Position - side2.Position).magnitude
		end
	end
	-- SORT
	{INSERT CLOSEST ONE HERE}.BillboardGui.Enabled =true
end

And I was wondering if I could sort all magnitudes in a table.

Thanks.

I believe you should look towards the press E to sit resources on the dev forum as they achieve a similar function of finding the closest specific object to a player.

Also don’t do this:

Yeah, it’ll lag the player/server as there are normally a lot of objects within the workspace and you are trying to get them every wait(). I suggest using collection services to narrow down the objects you are trying to search for as seen within the thread above.

Cool, thanks.

I’ll have a look.