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