I’m using a for loop to get all the children. Currently, I’m using GetDescendants for this because FindFirstChild doesn’t work. I also have a second problem I need it to get only parts.
Here is the code:
for i,v in pairs(script.Parent:GetDescendants()) do
print(i,v)
end
function GetDescendantsOfClass(Parent, Type)
local Table = {}
for _, Object in ipairs(Parent:GetDescendants()) do
if Object:IsA(Type) then
table.insert(Table, Object)
end
end
return Table
end
you can use the function to get all descendants of class
you would need to call it like this
GetDescendantsOfClass(script.Parent, "BasePart")
the first argument is the parent and the second argument is the class type
If you want to get all of the instances with that type you can loop through the descendants and store the correct ones in a table:
local Objects = {}
for _, Object in ipairs(Somthing:GetChildren) do
if Object:IsA("Part") then -- If the object is a part ten this will pass
table.insert(Objects, Object)
end
end
I literally got back from school and tested it
I put this code inside serverscriptservice
server script
function GetDescendantsOfClass(Parent, Type)
local Table = {}
for _, Object in ipairs(Parent:GetDescendants()) do
if Object:IsA(Type) then
table.insert(Table, Object)
end
end
return Table
end
print(GetDescendantsOfClass(workspace, "BasePart"))