(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.
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.
Ooh, thank you. I’ve always used ipairs loops, I’ll be sure to switch to using that. 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.
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
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