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

How do I use GetDescendants() properly?

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!

2 comments

0

I think you have to do folder:GetDescendants()

0

Try my code below

3 answers

✓ Accepted answer

###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()

2 comments

0

This answers my previous question but also creates another one. What does the "v" mean in the for statement?

0

v is just the name of the current decendant, It makes a loop untill the last descendant in the folder

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

1 comment

0

You still didn't answer his other question.

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()

`

2 comments

0

Still returning nothing, but if the code worked properly it should be able to return every Folder by it's name correct?

0

You didn't answer his question. If you're not going to explain you might as well leave a comment.

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