How do I get how many children a part has and keep count of it

I want to identify and get a live update on how many parts a child has, so I can keep up with how many children I can have, so how would I get the amount?

1 Like

Getting all the children will need Instance:GetChildren() and this returns a table of the children so to get the items do this #Instance:GetChildren() the # returns the amount of items in the table

1 Like

So can I do something like this?

local BallFolder = script.Parent.Parent

local Amount = script.Parent

local Table = {}

while task.wait() do
	Amount.Value = #Table
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		if v.Name == "Ball" then
			table.insert(Table["_"])
		end
	end
end
local partToCheck = -- The part you want to check
print(#partToCheck:GetChildren()) -- Prints how many parts are in the part

-- When a part is added
partToCheck.ChildAdded:Connect(function()
   print(#partToCheck:GetChildren()) 
end)

-- When a part is removed
partToCheck.ChildRemoved:Connect(function()
   print(#partToCheck:GetChildren()) 
end)

Can I do this?

while task.wait() do
	Amount.Value = #Table
	
	script.Parent.Parent.ChildAdded:Connect(function(child)
		if child.Name == "Ball" then
			table.insert(Table["Ball"])
		end
	end)
	
	script.Parent.Parent.ChildRemoved:Connect(function(child)
		if child.Name == "Ball" then
			table.remove(Table[child])
		end
	end)
end

Put this outside of the ‘while’ scope unless you want a memory leak

EDIT: It also doesn’t seem like your script would work properly as in this part

You’re not inserting a value properly, nor inserting the child, just a string “Ball”.
You should replace the table.insert with table.insert(Table, child)


Also this wouldn’t remove the child at all since you can’t index values directly without using their numerical index
I’m not sure, but I think you could get the index by using table.find (e.g table.find(Table, child))? Then you can do table.remove(Table, index)


Also, it would be a good idea to not use a while loop to update, and instead let the functions inside the events update it

e.g
local thing = script.Parent.Parent

local Table = thing:GetChildren()
Amount.Value = #Table

thing.ChildAdded:Connect(function(child)
	if child.Name == "Ball" then
		table.insert(Table, child)
		Amount.Value = #Table
	end
end)

thing.ChildRemoved:Connect(function(child)
	if child.Name == "Ball" then
		table.remove(Table, table.find(Table, child))
		Amount.Value = #Table
	end
end)
1 Like

I read the whole thing, sry for late reply btw, just do this

local Table = {}

for _ , v in pairs(script.Parent.Parent:GetChildren()) do
	if v.Name == "Ball" then
		table.insert(Table , v.Name)
	end
end

Amount.Value = #Table
	
script.Parent.Parent.ChildAdded:Connect(function(child)
	if child.Name == "Ball" then
		table.insert(Table , Child.Name)
		Amount.Value = #Table
	end
end)
	
script.Parent.Parent.ChildRemoved:Connect(function(child)
	if child.Name == "Ball" then
		table.remove(Table , table.find(Table , Child.Name))
		Amount.Value = #Table
	end
end)
2 Likes

It works, but I continuously get this error and nothing gets removed.

Workspace.Balls.Amount.Change:24: invalid argument #2 to 'remove' (number expected, got Instance)  - 
1 Like

Oh right table.remove needs its “index”
Ill fix the script and will like your comment when i finish editing so you can copy it again, sorry for that!

1 Like

And btw, i think it will be better to add a different name to each part so when it gets removed it doesn’t remove the wrong one… There could be a mistake there, if it didnt remove till me and ill change it,

1 Like