For example, I want to clone a part every time a character touches a button. There are more than ten parts that are cloned. I want the cloned parts destroyed when parts are more than 5 parts. How do I do that?
One way to do it would be to store the parts in a table then check how many objects are in that table every time you create a new object.
01 | local itemTable = { } |
02 | function cloneObject(object) |
03 | local newObject = object:clone() |
04 | newObject.Parent = --wherever you need |
05 | --[[ |
06 | more lines specifying the object, etc |
07 | --]] |
08 | table.insert(itemTable, newObject) |
09 | if (#itemTable > number) then |
10 | for _, v in ipairs (itemTable) do |
11 | v:remove() --or however you want to "destroy" the object |
12 | end |
13 | end |
14 | end |