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

How do I use GetDescendants() properly?

Asked by 6 years ago

This has been an important tidbit of code I've needed to learn how to use for a while. Can I use this function to find specific classes (i.e. Part, Folder, Script...), and if so, how? Here's what I've tried so far:

local sP = script.Parent

function totaling()
    wait(4)
    local wins = sP.AllStats:GetDescendants("Folder")

    print(wins)
end



totaling()

Unfortunately I can't figure out what I'm doing wrong because when I run this it doesn't return anything. Thank you for your time!

0
I think you have to do folder:GetDescendants() seith14 206 — 6y
0
Try my code below seith14 206 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Instance:GetDescendants() returns a table of all children and descendants of an object. And yes, you can check if something is a Part, Folder, or Script.

local sP = script.Parent

local function totaling() -- avoid global variables and functions unless there's a good reason not to
    wait(4)
    local wins = sP.AllStats:GetDescendants() -- no arguments

    for _, v in pairs(wins) do
        if v:IsA("LuaSourceContainer") or v:IsA("BasePart") or v:IsA("Folder") then
             -- check if the descendant inherits from class or is that class

            -- Code
        end
    end
end



totaling()

0
This answers my previous question but also creates another one. What does the "v" mean in the for statement? Beastlance 22 — 6y
0
v is just the name of the current decendant, It makes a loop untill the last descendant in the folder seith14 206 — 6y
Ad
Log in to vote
0
Answered by
ozzyDrive 670 Moderation Voter
6 years ago

As you can see from the documentation, the GetDescendants function does not take any parameters. It returns an array of the descendants, and as any other table, you can iterate through it.

local descendants = workspace:GetDescendants()
for _, descendant in next, descendants do
    print(descendant)
end
0
You still didn't answer his other question. User#19524 175 — 6y
Log in to vote
-1
Answered by
seith14 206 Moderation Voter
6 years ago
Edited 6 years ago

Try this

local sP = script.Parent

function totaling()
    wait(4)
    local wins = sP.AllStats.Folder:GetDescendants()
    for _, i in pairs(wins) do
    print(i.Name)
    end
end



totaling()

`

0
Still returning nothing, but if the code worked properly it should be able to return every Folder by it's name correct? Beastlance 22 — 6y
0
You didn't answer his question. If you're not going to explain you might as well leave a comment. User#19524 175 — 6y

Answer this question