local function setCollisionGroupRecursive(object)
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, playerCollisionGroupName)
end
for _, child in ipairs(object:GetChildren()) do
setCollisionGroupRecursive(child)
end
end
local function onCharacterAdded(character)
setCollisionGroupRecursive(character)
end
I found this piece of code while reading through this article. If I were to code this my way, I would loop through all the descendants of the character with character:GetDescendants(). My question is, is looping through parts in a model better using the article’s code, or is using :GetDescendants() better (or the same thing)?
in this situation they both have the same runtime since the recursion loops through all of the descendants as well. however for the recursive function if you know how the object is set up you could filter out some instances inside the object. (for instance if you know that the given object has parts inside them and inside each part there are only non baseparts. in this case using recursion with filtering baseparts only could lead to less runtime than :GetDescendants())
I’d find that, for most cases where I need to use :GetDescendants(), I don’t know which parts to filter out. I would consider this a case of micro-optimization. But at least I know that they both have the same performance implications.