Optimal way of finding all closest attachments to a point

(Do note, I am currently testing this right now, this page was mostly made to find alternatives)

I’m trying to find the optimal way of finding every attachment that is within x amount of studs. Things I have thought of: (Do note I use collection service to restrict the group of attachment’s numbers)

.Magnitude w/ loop

Pros: Simple
Cons: Requires me to loop through every attachment first, and if there are hundreds 
needing to be updated every frame that may impact performance.

GetPartsInRadius

Pros: No need for as big of a loop, I can just find the parent of each attachment,
find all of the attachments within it and check their positions.
Cons: I have not actually benchmarked GetPartsInRadius, I would imagine it
would be a bit performant (especially with a whitelist) However Im not sure whether
it would be the most performant method.

Or, maybe a mix of both? Possibly if the number of attachments are greater than 20, then I use GetPartsInRadius. Otherwise Magnitude? Please let me know better ways of doing this, or if I am overthinking.

Sorry, I may have misworded my post. I was just trying to find the most optimal way of finding every attachment nearby. Thank you for your time though :+1:

I said in my post that I used collection service to group the attachments I actually want it to effect on, although this wasn’t really a problem I was looking for I’ll look into it.

So the most optimal way of doing this would be using GetPartsInRadius and then performing an iteration loop on the descendants.

for i = 1, #descendants do
descendants[i] -- to grab the insstance
end

This loop is faster than looping with an ipairs loop because its not exactly looping through instances but looping with a number.

1 Like

Ooh, thank you. I’ve always used ipairs loops, I’ll be sure to switch to using that. :smile: I will also do the GetPartsInRadius thing as you said, + whitelist. Thank you! I will give you solution tomorrow or the day after to see if anyone else finds an even better way. I seriously do appreciate it though.

Just wondering, do you have all the attachments already tagged with collection service?

Yes, that is what I plan on doing. Would it be more efficient to tag their parents instead?

Well, if you are trying to find the closest ones that have that tag you essentially could just use an iteration loop getting all the ones that were tagged and compare which ones are the closest.

local ClosestAttachment
local ClosestValue = 2e9
local Collection = CollectionService:GetTagged("Tag")
for i = 1, #Collection do
	local attachment = Collection[i]
	local distance = (attachment.Position-Location).Magnitude
	if ClosestValue < distance then
		ClosestValue = distance
		ClosestAttachment = attachment
	end
end
1 Like

GetPartsInRadius is actually 3x slower than doing a magnitude check. The meta is something like the one below

code from @ScriptingVoid

local ClosestAttachment
local ClosestValue = 2e9
local Collection = CollectionService:GetTagged("Tag")
for i = 1, #Collection do
	local attachment = Collection[i]
	local distance = (attachment.Position-Location).Magnitude
	if ClosestValue < distance then
		ClosestValue = distance
		ClosestAttachment = attachment
	end
end
1 Like

Ah, even with (for example) hundreds of attachment points? + Whitelist on the getpartsinradius? Not trying to rebuke, just interested :sweat_smile:

Yes, even with whitelist.

This is using an example with 100 parts:

1 Like