:GetChildren() argument for filtering parts by name

Would be really nice to be able to use :GetChildren to find objects that have a similar name using an argument.

Model:GetChildren(“Apple”) would return a table of every object inside “Model” named “Apple”.

I know I could create a function for this, but it would be a lot more efficient to do it this way instead of looping through the children and checking each object’s name.

Figured with the addition of FindFirstChildOfClass, it shouldn’t be too hard to implement, right?

Just a small quality of life thing is all ^.^

7 Likes

Interesting feature request - though the looping method you mention is already quite efficient, plus it allows for more tweaking variables while checking.

local model = Model:GetChildren()
local apples = {}

for i=1,#model do
   if model[i].Name == "Apple" or model[i].Name == "apple" then
   table.insert(apples, model[i])
   end
end
4 Likes

So, my thoughts:

If we get something like this, it should probably use regular expressions like the string manipulation methods so it’s more powerful, rather than just plain text.

Also, if we were to get something like this, I’d definitely prefer a generic method for GetChildren/FindFirst i.e.

workspace:GetChildrenMatching({
   Name = "%d%w+";
   IsA = "BasePart";
   Transparency = "< 0.5"; 
})

or

workspace:FindFirstMatching({
   Name = "%d%w+";
   IsA = "BasePart";
   Transparency = "< 0.5"; 
})

Because that would be substantially more powerful. But as @dragonfrosting mentioned, I’m not sure we’d need this in the first place – if you have so many objects you need to search through that performance becomes a concern, you should probably work on grouping them better so you’re only looking through 500 parts instead of 5,000,000.

12 Likes

I agree, string matching would make the function a lot more powerful.

I know this probably wouldn’t be a massive improvement to performance, but when I have a table of 300 or so items and I only need to find 2 of them is bugs me that I have to create a loop and go through each one of them, and check the name of every single item. An extra 298 if then’s just seems unnecessary, ya know?

There are a lot of instances where I would only need to do this once or twice too, and having to make a loop for this again just seems so messy to me.

This is more of a quality of life thing for me than anything else of course, on the performance side I’m sure the difference would be small. But, since it probably wouldn’t be hard to implement I thought It might be worthwhile to ask :smiley:

I guess that makes sense. When I try to think through the times where I have had this problem of needing to find like-named objects, most of the time there is a way for me to find another more efficient solution as you’ve said.

Course in a lot of these situations (For example, an inventory that can hold different items of the same name) I feel like it would be much easier to simply have a function such as this, but I can’t really argue about it if this api would only be relevant in situations where the better fix would just be to do it in a different way.

So, case closed then. Thanks everyone for sharing your thoughts :smiley:

If performance is a big problem, you would probably want to keep a cached table with all the apples anyway. The small performance gain for filling the cache would be almost unnoticeable, especially if it happens at the start of the server, when there are no players and no real game logic going on.

1 Like

Yeah, which is why this was mostly for ease of use sake than actual performance gains. I don’t think it would really be worth the effort in the end to do optimizations like this, which is why I thought it might be a decent idea just to add this in to speed things up a bit, but again considering that you could almost always just program the game better to avoid needing to use this, I think It’s probably not worth the effort to add in the api in the end.

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