Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I achieve two :Methods in one check?

Asked by 4 years ago
Edited 4 years ago

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?

0
Are you trying to find a specific part? firestarroblox123 440 — 4y
0
of a specific class? firestarroblox123 440 — 4y
0
What are you trying to achieve here, just to make code shorter? and if so then the top method is probably the fastest if you want to get all parts in workspace.M poke7667 142 — 4y
0
Sorry, I meant shortest not fastest. poke7667 142 — 4y
View all comments (4 more)
0
I want insert all "Part" class types from a Model with multiple descendant objects without having to iterate through the model. I want to use Roblox's methods to achieve this. A method I wish Roblox had was :GetDescendantsThatAreParts <--. Metabasis 0 — 4y
0
@poke7667 Yes, I'm trying to achieve the alternative shortest possible method of doing this, but my question technically is if it is even possible to do two methods in one go? Metabasis 0 — 4y
0
There is no way faster firestarroblox123 440 — 4y
0
Also no, it isn't firestarroblox123 440 — 4y

2 answers

Log in to vote
0
Answered by
starmaq 1290 Moderation Voter
4 years ago

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

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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"))()

Answer this question