Closest attachment to a part

What do you want to achieve? Return one closest attachment

What solutions have you tried so far? I checked other forums

How can I find the closest attachment to a part?
(I used translator, so i can have mistakes in my text)

try this

local attachmentList -- all of your attachments here
local targetPart -- the part you want to check with

local closestAttachment = nil
for _,attachment in attachmentList do -- go through your attachment list
 if closestAttachment == nil then
  closestAttachment = attachment -- set the first attachment as closest
 else
  if (targetPart.Position - closestAttachment.WorldPosition).Magnitude > (targetPart.Position - attachment.WorldPosition).Magnitude then -- compare the distance between targetPart and the currently closest attachment with the distance between targetPart and the attachment
   closestAttachment = attachment -- if attachment is closer than set it as the new closestAttachment
  end
 end
end

print(closestAttachment) -- will print out the closest attachment
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.