generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Get all parts in Workspace

Previous Thread :: Next Thread 
Negativize is online. Negativize
Joined: 02 May 2011
Total Posts: 25068
19 Apr 2017 06:35 PM
Is there a way to get every part in Workspace ignoring Models and Folders and that sort of stuff?
Report Abuse
Negativize is online. Negativize
Joined: 02 May 2011
Total Posts: 25068
19 Apr 2017 06:38 PM
I'm assuming you would get all parts like normal, and if said part is a model or a folder or whatever get ITS children, and repeat until you have every part
Report Abuse
Cyrakohl is not online. Cyrakohl
Joined: 09 Nov 2014
Total Posts: 3197
19 Apr 2017 06:53 PM


for _,v in pairs(workspace:GetChildren()) do

if v:IsA("BasePart") then
v:Destroy()

end


R$117
Report Abuse
Negativize is online. Negativize
Joined: 02 May 2011
Total Posts: 25068
19 Apr 2017 06:58 PM
'for _,v in pairs(workspace:GetChildren()) do

if v:IsA("BasePart") then
v:Destroy()

end'

This gets all parts in Workspace only. I want to retrieve every single part in Workspace regardless if the parts' Parents are models or folders or whatnot
Report Abuse
AxonMega is online. AxonMega
Joined: 29 Aug 2014
Total Posts: 2403
19 Apr 2017 07:01 PM
local function findParts(object, list)
for _, child in ipairs(object:GetChildren()) do
if child:IsA("BasePart") then
table.insert(list, child)
else
findParts(child, list)
end
end
return list
end

local parts = findParts(workspace, {})
Report Abuse
jordan0810 is not online. jordan0810
Joined: 03 Jun 2012
Total Posts: 846
19 Apr 2017 07:03 PM
local function getChildren(parent) for _, v in pairs(parent) do if #v.GetChildren() >= # #### ############## else print(v.Name) end end end getChildren(workspace)
Report Abuse
Zed_YT is not online. Zed_YT
Joined: 04 Oct 2016
Total Posts: 25
19 Apr 2017 07:40 PM
local Count = 0;

function B(x)
for i,v in pairs(x:GetChildren()) do
if x:IsA('BasePart') then
Count = Count + 1;
else
B(v)
end
end
end

B(game.Workspace)

print(Count);


Report Abuse
iiJaguar_Cub is online. iiJaguar_Cub
Joined: 28 Jun 2016
Total Posts: 627
19 Apr 2017 08:09 PM
^^^
Ayyy It's Zed!


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
19 Apr 2017 08:17 PM
Using recursion to do this is not beautiful.
And Zed he wants to GET THEM not GET HOW MANY THERE ARE.

local function GetAllPartsInWorkspace()
local s = { workspace }
local p = { }
local idx = 1
while idx <= #s do
local v = s[idx]
if v:IsA("BasePart") and v ~= workspace.Terrain then
table.insert(p, v)
end
for _, child in ipairs(v:GetChildren()) do
table.insert(s, child)
end
idx = idx + 1
end
return p
end

This is nicer not only because it's not recursive (which is normally "fine" but tail-call optimization was stripped away in Roblox) but it acts in an order of how far things have been nested. So the order will be all parts in workspace, then all parts nested once from workspace, then twice, etc. (row by row) instead of column by column.
Report Abuse
samy22 is not online. samy22
Joined: 28 Sep 2008
Total Posts: 2181
19 Apr 2017 08:49 PM
That is a beautiful solution, I must say
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
19 Apr 2017 09:00 PM
#### https://forum.roblox.com/Forum/ShowPost.aspx?PostID=149132541
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
19 Apr 2017 09:01 PM
Oh boy 2014 me was pretty damn dumb
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
19 Apr 2017 09:02 PM
not as dumb as me using a for loop like I did lmao
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
19 Apr 2017 09:31 PM
Really it's just implementing breadth-first search non-recursively. Sadly the recursive method most people use is pmuch just depth-first search which makes it ugly. It goes through all possible descendants of a given child recursively first before moving on the the next one. puu.sh/vpWYx/eeb1072c3e.png

I mean both get the job done but IMO breadth is much nicer for this situation even though realistically the order doesn't end up really mattering.

Here's a nice visualization (parts under parts imply it's a child of it):
Left is using BFS, right is using DFS. Although I didn't recursively do any of these, the one on the right is what you see when you do it recursively (well, the method most people use since you can technically implement BFS recursively).

puu.sh/vpXYJ/30597958a9.mp4
Report Abuse
samy22 is not online. samy22
Joined: 28 Sep 2008
Total Posts: 2181
19 Apr 2017 09:44 PM
I also think its more useful because you can choose to stop searching past a certain "level" which can be utilized. With the recursive search, it is more complicated.
Report Abuse
ImVortexe is not online. ImVortexe
Joined: 26 Jan 2013
Total Posts: 1149
19 Apr 2017 09:51 PM
ignore = {"Model", "Folder"}
for i =1,#ignore do
for i,v in pairs(workspace:GetChildren()) do
if v.Name~=ignore[i] or v.ClassName~=ignore[i] then
--do stuff?
end
end
end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
19 Apr 2017 09:55 PM
well it'd still be pretty simple using the recursive method:
(not tested but the logic should be correct)

local function GetPartsRecursive(parent, maxNest)
local parts = { }
local function get(parent, nest)
if nest > maxNest then return end
if parent:IsA("Part") then parts[#parts + 1] = parent end
for _, child in ipairs(parent:GetChildren()) do
descrec(child, nest + 1)
end
end
descrec(parent, 1)
return parts
end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
19 Apr 2017 09:57 PM
"descrec" should be "get" woops
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
19 Apr 2017 10:06 PM
GetParts = function(obj,class)
local parts = {}
for i,v in next, obj:GetChildren() do
if v:IsA(class) and v.Name ~= 'Terrain' then
table.insert(parts,v)
end
for i,v in next, GetParts(v,class) do
table.insert(parts,v)
end
end
return parts
end

local parts = GetParts(game,'BasePart')

for i,v in next, parts do
print(v,v.Parent)
end


Report Abuse
samy22 is not online. samy22
Joined: 28 Sep 2008
Total Posts: 2181
19 Apr 2017 10:06 PM
What about a minimum nest level to a maximum? :p
Nah but I meant it's more intuitive and easier to understand by someone reading the code (if you were to use minima/maxima levels that is).
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
19 Apr 2017 10:08 PM
Yeah I mean you can still implement a minimum by just not adding it to the table unless nest >= minimum
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
20 Apr 2017 12:45 PM
cntkillme that was a really cool way to show how they work
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image