You can connect every textbutton in a table with the same event like this:
for _, button in pairs(textbuttons) do
button.MouseButton1Click:connect(function()
print(‘clicked!’)
end)
end
But I want to add a script to every child in a table. All children are instances like models or parts and I want this function or script to run whever i add it to them. I was wondering If i could loop through the table and use task.spawn() to add a task to every child but thats just too laggy. Next thing I tried was creating a disabled script that I cloned to every instance and then enabled it. But that doesn’t work because for some reason they get cloned twice. Is there any other way to clone a script or function to an instance and run it when duplicated?
if you mean to give the function to the objects that you added in the table you can use metatable for that
local tbl = {"1"}
local mt = {
__newindex = function(t, key, value)
rawset(t, key, value)
print("you added ", key, value) -- runs when a new child is added to a table
end
}
setmetatable(tbl, mt)
replace the print statment with what you want to give the function
another simpler way is to use collectionservice it has built in event that fires when a child is added
local CollectionService = game:GetService("CollectionService")
CollectionService.ChildAdded:Connect(function(child)
end)
if you mean to add a script to every child that would be waste of memory instead use task.spawn() wouldnot be laggy and if it will be laggy it will be less laggy than having multiple scripts