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

Creating something like a FindFirstDescendant?

Asked by 4 years ago

Is there a way I can create something that's similar to a hypothetical FindFirstDescendant? I know such a thing doesn't exist, but I have multiple models where, in order to use FindFirstChild, I would have to dig through the many models INSIDE the models, and give each one a unique name. Is there a way I can avoid doing that, and simply say, within a script, to find a brick by a certain name, that's a descendant somewhere in Workspace?

Right now, here's the relevant script...it's a LocalScript, if that makes a difference:

local plr = game.Players.LocalPlayer
local dour = workspace:FindFirstChild("TTDoor1") --Not actually a child; it's a descendant somewhere
print(dour.Name)

2 answers

Log in to vote
0
Answered by 4 years ago

I guess this is what you want.

for _, v in pairs(workspace:GetDescendants()) do
    if v.Name == "TTDoor1" then
        print(v)
        v.BrickColor = BrickColor.new("Bright green")
    end
end
0
^ Do you think a break could work also, to prevent it going higher and looking for more? pwx 1581 — 4y
0
No, you don't need to add **break, return** and **debounce** to stop the loop unless if you have more objects that are same names. For examble you have 4 **TTDoor1** object the output will be **TTDoor1 (4)**. TankArgie_DEV 0 — 4y
0
So to use it as a variable to later use in an if statement, would I just say "local dour = v" inside that v.Name=TTDoor statement? SuperJumpman12 43 — 4y
0
You don't need to add variable. You can replace the "v" into "dour". TankArgie_DEV 0 — 4y
0
But if you want to make a variable of your door you need to make a table and decode it into an object. If you have so many "door" and you just put a normal variable, only just one can be handled. TankArgie_DEV 0 — 4y
Ad
Log in to vote
0
Answered by 3 years ago

Instead of FindFirstDescendant(descendant), do FindFirstChild(descendant, true).

Answer this question