Hello devs, so I am working on my pathfinding module and I need to get all parts in workspace, yes I know there is :GetChildren() and GetDescendants() but these won’t work if ladder is in Model, that is in another model, you get the point, so is there a way to get all parts in workspace?
local CubeModel = game.Workspace.Cubes
local AllCubes = CubeModel:GetChildren()
print(#AllCubes) -- This Show How Much Part/Cubes Are In Model.
for i, cube in pairs(AllCubes) do
-- You Can Add Some Functions To All Cubes Or Parts.
-- Like:
cube.BrickColor = BrickColor.new("Really red")
end
You don’t get my point, I said I need to get all parts in workspace, not in model.
Hey, I don’t see why GetDescendants wouldn’t work?
GetDescendants would get all descendants of the workspace. Example: If you have a map (as a model) inside of your workspace, and inside of the map there is a ladder (which is a model), the ladder would also be returned by GetDescendants
Its because if Truss is in Model, that is in Model, it wouldn’t work cuz it would get descendants, not parts.
So you only want to look at parts inside of the workspace and not folders, models etc?
I wan’t to get all workspace’s parts, so entire workspace.
for i,v in pairs(game.Workspace:GetDescendants()) do
if v:IsA("Part") then
v:Destroy()
end
end
I don’t know why this would not work?
workspace:GetDescendants() will return all parts in the workspace and every other Instance. If you want to detect if it’s a BasePart, you can always do a if v:IsA("BasePart")
statement.
A BasePart
is the base-class for all part-related instances, such as TrussParts and MeshParts.
If you only want to get the parts, first you use workspace:GetDescendants, then use a for loop to loop through the table. Then check if it’s a Part, and if it is, do whatever you want.
for _, part in pairs(workspace:GetDescendants()) do
if part:IsA("BasePart") then
-- do whatever you want.
end
end
I said I need to get all of the workspace’s parts, and you don’t get my point, why GetDescendants() or GetChildren() won’t work, its because of Truss’ parent, if truss is in Model that is in etc. folder, It doesnt detect truss.
It would. Did you already test it out with GetDescendants and it didn’t work?
That’s exactly what GetDescendants does, though. It loops through every instance in the workspace. As long as the Workspace is an ancestor of the BasePart, it will detect it.
GetChildren will only return its direct children. GetDescendants will return it’s children, it’s children’s children, and so on.
fixed it
for i,v in pairs(game.Workspace:GetDescendants()) do
if v:IsA("BasePart") then
if not v:IsA('Terrain') then
print(v.Name)
end
end
end
Let us look at this example.
You see, MyAmazingPart is inside of a truss inside of a map.
Try, using a for loop
for i,v in pairs(game.Workspace:GetChildren()) do
if v:IsA(“Part”) and v.Parent == game.Workspace then
–the part is located in workspace and is a part
end
end
Look into the CollectionService - you can tag certain objects of significance pretty easily and cuts down on script resources to get this list of ladders you desire.
CollectionService (roblox.com)
local collectionService = game:GetService("CollectionService")
local parts = collectionService:GetTagged("PartsToGrab")
local other = collectionService:GetTagged("PartsNotToGrab")
Suggested plugin:
Tag Editor - Roblox
local allParts = {}
function fetchParts()
for i, v in pairs(workspace:GetDescendants()) do -- Filter through workspace's descendants
if v:IsA("BasePart") then -- If v is a Union, Mesh or Part
table.insert(allParts, v) -- Put the instance into allParts
end
end
return allParts
end
function printLen()
fetchParts() -- Call fetchParts
print("There are "..#allParts.." BaseParts") -- "There are [len] BaseParts"
end
printLen() -- Call printLen()
You can swap BasePart
for Part
or UnionOperation
, depending on what specifically you want to look for
Guys, :GetDescendants() worked! I didn’t know that this is how :GetDescendants() works!