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

How to iterate through entire game?

Asked by 8 years ago

How do i iterate through everything in game and their children?

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

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.

0
I saw that thread too. And i just want to learn how he does it without getting an error(like me) Demon_ster 67 — 8y
0
Well, what does the error say? Pyrondon 2089 — 8y
0
"An error occurred". I am pretty sure thats because of a robloxlocked item Demon_ster 67 — 8y
0
Yes Demon_ster 67 — 8y
Ad

Answer this question