Help me in Script

Hi there!
I want to know " How can i find the number of Parts in a Group with Script."

2 Likes
local object = your object
local countOfParts = #object:GetChildren()
print(countOfParts)
2 Likes

what I really want is to find the number of parts in a group , model or folder.

Thank you very much
ı will try it

thank you very much for your attention

I changed code a bit, you can try it now.

okay
ı will try it
thank you Have a nice day

If your model/folder has models in it, and you want to count the BaseParts inside that model, then you could do

local countOfParts = 0
for _, v in ipairs(object:GetDescendants()) do
    if v:IsA("BasePart") then
        countOfParts += 1
    end
end
print(countOfParts)

Thank you very much for your attention
ı will try it but can you explain me whats mean (for_, v in ipairs(object:GetDescendants()))

I’m going to assume what you mean by ‘Group’ here is a Model object.

To get the amount of objects contained in a model (or any other instance which can store objects in the explorer like Folders for example), we can either use :GetChildren() or :GetDescendants().

:GetChildren() returns a dictionary/table of objects which are children of the container, ‘Group’ in your case.

https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren

:GetDescendants() returns a dictionary/table of objects which are ‘childrens children’ of objects, that was the best way I could put this into words :joy:

https://developer.roblox.com/en-us/api-reference/function/Instance/GetDescendants

A few images will explain this well, I’ve done this on my game project so my apologies for the ‘Tracks’ folder there. Just pay attention to the ‘Group’ model.

Lets consider this code:

local groupModel = game:GetService("Workspace"):WaitForChild("Group")
local groupModelChildren = groupModel:GetChildren()

The :GetChildren() that we used on ‘groupModel’ will return a dictionary of objects containing ‘Child’ and ‘Part’.

GetChildrenImage

Now we’ll use :GetDescendants() instead of :GetChildren()

local groupModel = game:GetService("Workspace"):WaitForChild("Group")
local groupModelDescendants = groupModel:GetDescendants()

This would return ‘Child’, ‘Part’, but it would also return the children of child objects (if it has any) and return them as part of our ‘groupModelDescendants’ dictionary. So if yet another model with a part in were contained in ‘Child’, it would also return the objects within that.

GetDescendantsImage

In our case, ‘groupModelDescendants’ is a dictionary containing ‘Child’, ‘Part’ and the ‘Part’ within Child.

Now that we have our objects, how can we get the number of objects in our dictionary, this is where the ‘#’ symbol comes in, to get the length of a table (how many elements, objects, instances etc) are stored, we can simply do.

local groupModel = game:GetService("Workspace"):WaitForChild("Group")
local groupModelChildren = groupModel:GetDescendants()

print(#groupModelDescendants)

Whether you use :GetChildren() or :GetDescendants() entirely depends on the use-case that you’re aiming for, whether you only want to know how many instances are in ‘Group’ or how many instances are within children’s children etc.

That’s the best way I could put this :joy:, if you have any questions please do let me know! :slight_smile:

I,v in pairs is basically doing a loop. _,v in pairs is the same thing as integer, value(i,v) just different code. It’s basically looping through the descendants. I is the integer(int) value and the count. V is the descendant.

Thank you for your attention and your answer.

game:GetService("Workspace") :thinking:

Workspace and all servces in explorer are already pre-defined and you dont need to get the pre-defined services by saying game:GetService(). game.Workspace is better way to get it. Not a big difference, but it is better.

Thank you very much for your attention and pretty long answer :laughing:

I disagree, workspace is a way faster way of doing it.

Still does the same thing that I want though, so does it matter? :wink:

I usually add that as a variable above my scripts, usually assigning it to ‘WS’, which is easier for me to write obviously. It’s a much better way to get the actual instance of it too.

:man_shrugging:

P.S: By above, I mean at the top of my scripts, should have been more specific there.

No problem! Good luck with whatever you’re using this information for. :grin: