Hi, I know the answer to what I want to achieve but I was wondering if there's a shorter way of doing this (I'm trying to use as few characters as possible):
k=workspace.M:GetDescendants() for i=1,#k do if k[i]:IsA("Part")then print("y")end end
A faster way would be to do something like this:
k=workspace.M:GetDescendants():IsA("Part")
But the example above does not work. How would I do that?
If this worked, the table k would contain all part descendants without me having to write a for loop.
EDIT: I'm trying to achieve the alternative shortest possible method of doing this, but my question is if it is even possible to do two methods in one go like my example?
For your main question, no a method like that doesn't exist.
~~~~~~~~~~~~~~~
k=workspace.M:GetDescendnts():IsA("Part")
~~~~~~~~~~~~~~~~
And this way of writing things isn't correct, becuse technically workspace.M:GetDescendants()
is returning a table, and tables don't even have hods, and even if they did a IsA()
wouldn't make sense.
And if you're looking for a faster, really there isn't, unless you get really creative
If your goal is efficiency, then the code blow is your best bet. I think the only thing you could do to further optimize this outside of turning to another language is removing the function call. Code runs faster if you don't put it in a function, but it's more tedious to write.
local function getDescendantsWhichAreParts(obj) local parts = {} local descendants = obj:GetDescendants() for index = 1, #descendants do local value = descendants[index] if value.ClassName == "Part" then parts[#parts + 1] = value end end return parts end local parts = getDescendantsWhichAreParts(workspace.M) --Table of descendants which are parts
If you're trying to do it in as few characters as possible, the code you had originally is probably your best bet. Alternatively you could put this function in a module script and require it from your other scripts, so you could do the same thing in one line.
Module Script:
--Module script is named "getDescendantsWhichAreParts" and is located in replicated storage return function(obj) local parts = {} local descendants = obj:GetDescendants() for index = 1, #descendants do local value = descendants[index] if value.ClassName == "Part" then parts[#parts + 1] = value end end return parts end
Server Script:
local parts = require(game:GetService("ReplicatedStorage"):WaitForChild("getDescendantsWhichAreParts"))()