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?
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
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)
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)
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) -
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!
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,