Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find all children with filter?

So what I mean by this is how would I find all the children named 'THIS' in this model? And if it is called 'THIS' execute this function?

I know how to find all the children of a part but don't know anything past that...

local children = model
for i = 1, #children do

--if childs names is THIS then 
--rest of code

Of course, if there is a more efficient way that would be appreciated.

6 comments

0

Well your comment is correct already

0

Isn't there something like workspace _,v in pairs do? Could I use that method, and how would I?

0

Also if I am using this method how would I interact with that specific child?

0

the in pairs functions loops through all the children of the model, if you already got the table of the model's children already your code will work fine

0

How would I set that child as a local?

0

what does that mean

1 answer

✓ Accepted answer

Here I suppose you know how to get the children of the model already so i will just leave that for you to write, store it in the table i named "children"


for i =1,#children do
	wait()
	if children[i].Name == "this" then--your if statement
		print("this is found at index"..i)--this code will run if the part is named this, and shows you the index of the part in the table
	else
		print("this is not found at index"..i)--this code will run if the part is not named this, and shows you the index of the part in the table
	end
end

Alternatively with the in pairs loop, both will work

for i,v in pairs(children) do
	
	if v.Name == "this" then
		print("this is found at index"..i)
	else 
		print("this is not found at index"..i)
	end
end

5 comments

0

But what if there are multiple instances named 'this'?

0

I wanted to find a specific 'this' which has a child called 'NEWTHIS'.

0

If there are say, 2 parts named "this", and they are the first and third part of the model, then it will print it out two times and say this is found at index 1 and this is found at index 2

0

CORRECTION for my above comment, it will print out this is found at index 1 and this is found at index 3, if you want to make sure "this" has a child named "newthis" just add another if statement

0

use the method FindFirstChild(), if you dont know what that is here is the wiki for it: https://www.robloxdev.com/api-reference/function/Instance/FindFirstChild

This discussion is preserved as it was. New answers, comments, and votes are closed.