Aquiring all parts with a name and number instead of everything else?

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

2 Likes

attach.Name..a doesn’t check for attatchment “A1” for example, it just says attatch.Name + a.

You should find a better way to check for them.

1 Like

I’ve tried by using attach.Name = "A"..a but finds no results in total

2 Likes

Hmmm… what are you trying to do with the attachments?

1 Like

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

1 Like

Just to make sure, attach.Name = "A"..a would be an assignment operation (setting the name of attach to "A"..a).

Checking if the name of attach is equivalent to "A"..a would be written as attach.Name == "A"..a (with == instead of =). Is this what you mean?

1 Like

oh whoops yeah i forgot about the extra = here but i did use the == in the process with no results

1 Like

Try doing this:
if string.gsub(attach.Name, "%d+", "") == "A" then

This will check if the name is just an A, not checking any numbers.

1 Like

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
2 Likes

oh sweet thanks bro, do you have a source where i can find more in detailed stuff like this? I’ve tried the docs but they feel a bit lacking

1 Like

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.

1 Like

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.

4 Likes

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