How do i iterate through everything in game and their children?
How to iterate through entire game?
1 answer
I wasn't sure on this one, so I looked it up and found a result for it. Full thread is here.
local function GetDescendants(root, classes, tab)
local tab = tab or {}
for _, class in pairs(type(classes) == "string" and {classes or "Instance"} or classes or {"Instance"}) do
for index, child in pairs(pcall(game.IsA, root, "Instance") and root:GetChildren() or {}) do
if ({pcall(game.IsA, child, class)})[2] == true then
tab[#tab + 1] = child
end
GetDescendants(child, class, tab)
end
end
return tab
end
for _, v in pairs(GetDescendants(game)) do
print(v)
end
From the person who answered: "root is the starting instance. It is not included in the returned results. (i.e. GetDescendants(workspace) would not pass back a workspace reference)
classes: Are you looking for something in particular? (i.e. "Script" or "Part") You can pass a classname as a string (i.e. GetDescendants(workspace, "Script")) You can pass multiple classnames in a tabke (i.e. GetDescendants(workspace, {"Script", "Basepart"}))
In general, you don't initialize tab."
I'm not going to lie, I'm not 100% on this one, and I don't know what you need it for, but there you go.
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
I saw that thread too. And i just want to learn how he does it without getting an error(like me)
Well, what does the error say?
"An error occurred". I am pretty sure thats because of a robloxlocked item
Yes