Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I achieve two :Methods in one check?

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?

8 comments

0

Are you trying to find a specific part?

0

of a specific class?

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

0

Sorry, I meant shortest not fastest.

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 <--.

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?

0

There is no way faster

0

Also no, it isn't

2 answers

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

This discussion is preserved as it was. New answers, comments, and votes are closed.