Making "for i, v in pairs" more tidy

Was wondering if there was a way if I could neaten my code?

for i, v in pairs (script.Parent.Parent.Parent:GetDescendants()) do 

	if v.ClassName == "Part" or v.ClassName == "Union" or v.Name == "Union" or v.ClassName == "Wedge" or v.Name == "EnahancedCone" or v.ClassName == "Seat" or v.ClassName == "MeshPart" then 
		v.Anchored = false
		v.CanCollide = false

	end
end

Doing this every time I want to change something is a little bit untidy in my code and I was wondering if (like a function) I could make it so instead of all that code I only have to call one line. And so every time I call it I can change the code in the brackets (script.parent) without pasting the whole code again. Any help is much appreciated

What is your goal that you are trying to accomplish?

it appears this is almost certainly not the way to go about it

It’s easier to simply do v:IsA(“BasePart”), since all parts inherit from BasePart.

Pro tip: You can just use if v:IsA("BasePart") == true to shorten the code by using class inheritance information of instances.

Use v:IsA('BasePart') instead since that covers Unions Mesh parts etc.

ok like 10 other people just said the same thing

does the code work if yes the it need to be #help-and-feedback:code-review

So, can I do

v:IsA("BasePart") or v:IsA("whatever"). 

Or does that only apply for 1 type of part. And also, how can I change the script.parent bit in the brackets everytime? Edit: I read wrong, basepart apparently covers meshes and unions?

Yeah but for you case, any other :IsA checks are unnecessary since :IsA("BasePart") covers all 3D physical parts (UnionOperations, Parts, MeshParts, ect.) already.

So, I would start off with

for i, v in pairs (script.Parent.Parent.Parent:GetDescendants())

and then, whenever I wanted to refer to it I could just do

v:IsA("BasePart?")

Yalls responses do not pass the vibe check :face_with_raised_eyebrow:

What you can do is create a function and pass through a table of objects

function NewFunction(table)
-- code
end

NewFunction(script.Parent.Parent.Parent:GetDescendants())

From there you will want to check all its part types

function NewFunction(table)
	for _, Object in pairs(table) do
		if Object:IsA("BasePart") then
			-- more code
		end
    end
end

NewFunction(script.Parent.Parent.Parent:GetDescendants())

Now your wanting to make that said object both Unanchored and Collidable

function NewFunction(table)
	for _, Object in pairs(table) do
		if Object:IsA("BasePart") then
			Object.Anchored = false
			Object.CanCollide = false
		end
    end
end

NewFunction(script.Parent.Parent.Parent:GetDescendants())

Now whenever you pass through a table of objects it will make it Unanchored and colliable.

3 Likes

Yes, without that question mark in the string.

Hello.
I have an answer for you. Read comments as they’re quite important.
In my opinion, the code below looks much more organized and neat.

local desc = script.Parent.Parent.Parent; -- I advise against doing .Parent chains. They generally look unprofessional and it's harder to locate the object that you're referencing

local classWhitelist = {"Part", "Union", "Wedge", "Seat", "MeshPart"}; -- this list holds all of the classes that should not be anchored and collidable
local nameWhitelist = {"Union", "EnahancedCone"}; -- this list holds all of the instance names that should not be anchored and collidable
--                               ^ Spelling error? (EnhancedCone)

--  _ doesn't have any impact on code, however it shows that you're not using this variable (index) and it cleans up the code a bit.
for _, part in ipairs(desc:GetDescendants()) do -- using ipairs here, because it should be used for arrays (lists) `{value}`, whereas `pairs` should be used for dictionaries `{[index] = value}`
    if table.find(classWhitelist, part.ClassName) or table.find(nameWhitelist, part.Name) then
        part.Anchored = false;
        part.CanCollide = false;
    end
end

So basically, instead of this long if chain, we can divide it into tables and then check if the part’s class name or name can be found in them, then the check passes and sets Anchored and CanCollide to false on that instance.

2 Likes

I think I will go with this method as it suits what I need.

But me_isidoit, your code may also come in handy, if I could mark 2 comments as solution I would. But both are really helpful, thanks a lot.

1 Like