UNSOLVABLE part array

so i got a folder, theres a part called !A!, now i want to know which part in the same folder is closest to A

1 Like

please give us more context, If you want to get the closest part to A in a folder, you can just do a simple for loop and comparison

local closestPart, closestDist = nil, math.huge

for _, part in folderParts do
   if part == A then continue end -- skip the iteration if part is A
   local dist = (part.Position-A.Position).Magnitude

   if dist < closestDist then
      closestPart = part; closestDist = dist end -- record the closest part and distance
end
1 Like
local folder = script.Parent --your folder path
local A = folder:WaitForChild("!A!") --your !A! path

local function diff(p1: BasePart, p2: BasePart): number
	return (p1.Position-p2.Position).Magnitude
end

--My function doesn't cache the closest distance in favor of readability 
--I consider the cost too little to care for it unless you're testing many parts
--And if you test many parts then you may want to consider a spherecast instead
local function findClosest(p: BasePart, group: {BasePart}): BasePart?
	local closest = nil
	for _, part in pairs(group) do
		if part == p then continue end
		if not closest or diff(p, part) < diff(p, closest) then
			closest = part
		end
	end
	return closest 
end

--Change :GetChildren() to :GetDescendants() if you want it to be recursive
--Basically :GetDescendants() makes it look for parts inside parts as well
local closest = findClosest(A, folder:GetChildren())
print(closest)
1 Like