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

How to iterate through entire game?

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

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.

4 comments

0

I saw that thread too. And i just want to learn how he does it without getting an error(like me)

0

Well, what does the error say?

0

"An error occurred". I am pretty sure thats because of a robloxlocked item

0

Yes

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