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
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!
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!