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

How do i make a script that gets EACH part in the game?

So i am making random stuff, and i was testing something. And i found a problem with what i was doing. I can't make something that gets each 'game' part.

For example of what i mean:

I want a script that gets every item (class) present in the place. How do i do that? I will be glad when i find out. Thanks

1 comment

0

This is not a request site, please post your attempted script.

2 answers

You can use the for statement to sort through children of the Workspace pretty easily.

Here's some code that will do what you're asking. I explained everything in comments.

a = game.Workspace:GetChildren() --Gets all of the Children of the workspace, regardless of ClassName.
bricks = 0 --This will change when the script finds bricks.

for i,v in pairs(a) do --Starts a for loop to run through the children of the workspace
	if v:IsA("Part") then --Checks if the Child of the workspace's ClassName is "Part".
	bricks = bricks + 1 --Adds 1 to the brick counter.
end
print(bricks) --When the script is done finding the Workspace's children, it will print the total number of bricks in the output.

Hope I helped!

1 comment

0

Lol, thank you, that i already know. Read again, please.

If you want to get every single object in the workspace, then I suggest this:

local ObjectTable = {}
function GetObjects(Area)
	for _,Obj in pairs(Area:GetChildren() do
		table.insert(ObjectTable,Obj)
		GetObjects(Obj)
	end
end
GetObjects(game.Workspace)

This will make table ObjectTable contain every single object in the workspace. However, if you wanted every PART in the workspace, then you would slightly modify the script, like so:

local ObjectTable = {}
function GetObjects(Area)
	for _,Obj in pairs(Area:GetChildren() do
		if Obj:IsA("BasePart") then --This makes sure Obj is a brick
			table.insert(ObjectTable,Obj)
		end
		GetObjects(Obj)
	end
end
GetObjects(game.Workspace)

Hope this helped!

3 comments

0

i want every part inside each part and each part and each part etc and in other services too. Thats what i mean by game! Please reply

0

Yeah, this function will do that. Just replace the "game.Workspace" with "game"

0

already tried that

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