Alright so im trying to get all the attachments with a number (A1, A2, A3,etc.) within a object by using using a function like this
local a = 0
function attach()
for _, attach in pairs(Object:GetDescendants()) do
a += 1
if attach:IsA("Attachment") and attach.Name..a then
print(attach)
end
end
end
and the result usually is me getting all the correct attachments such as A1,A 2, A3 but the only issue is that I end up receiving other other unnecessary attachments as well and I was wondering what was the issue with this code
Just trying to group them up in a script to better manage them all rather than to have to manually manage each one without having to deal with other attatchments
It’s probably because you add to a for every object, but not for every number within the loop.
function attach()
for _, attach in pairs(Object:GetDescendants()) do
for a = 1, #Object:GetDescendants() do
if attach:IsA("Attachment") and attach.Name == "A"..a then
print(attach)
end
end
end
end
There isn’t really a detailed doc for specific situations. There are docs for almost every method, but you would have to figure out how to use them together. Although, I guess you could consider the dev forum as the more special and specific docs, so search around here and you’ll find a lot of answers.
You could also implement the following code and check if the pattern matches the string.
string.match(attach.Name, "^A%d+$")
This checks if the name starts with the uppercase “A” letter and ends with a number (no matter how many digits it has.)
function attach()
for _, attach in pairs(Object:GetDescendants()) do
if attach:IsA("Attachment") and string.match(attach.Name, "^A%d+$") then
print(attach.Name)
end
end
end
Going through every descendant with the total number of objects may not be performant as to doing what @Kaid3n22 did in the script. Assuming the objects were a total of n, you would have done n² for loops just to check an attachment’s name.