Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

The "Tip of the day" Thing?

Asked by 9 years ago

"TIP OF THE DAY Did you know FindFirstChild has a second, optional, parameter which will search through every descendant?" How would I use this? I look all over the wiki.

2 answers

Log in to vote
5
Answered by 9 years ago

Lets say that this is the hierarchy or an object you are trying to find:

Workspace

Model

Banana

Normally to find the object Banana you would have to create a recursive function to search workspace. When the second argument is true FindFirstChild will recursively search the directory until it finds the object.

Workspace:FindFirstChild("Banana",true) --Would return Banana
Workspace:FindFirstChild("Banana") --Would not find Banana

The equivalent recursive function.

function Recurse(Location,Name)
    for _,n in pairs(Location:GetChildren()) do
        if n.Name == Name then
            return n
        end
        Recurse(n,Name)
    end
end

Recurse(Workspace,"Banana")
Ad
Log in to vote
6
Answered by
bobder2 135
9 years ago

The wiki says:

Instance FindFirstChild (
    string name,
    bool recursive = false
)

string name means the name of the child you want to find, this is the standard use. bool recursive = false this is the optional parameter that the tip of the day is talking about, set this to true and it will search every child for more children with the name. Example:

Here's your explorer

workspace
    Model
        Part

and an example script:

part = game.workspace:findFirstChild("Part",true)
part.Name = "Found You!"

Here's your explorer after the script runs

workspace
    Model
        Found You!

Answer this question