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