How do you make all buttons click events work with just one script?

Hello!

I am currently working on a plugin and I need to be able to access many different buttons that all do the same thing. Because I am making a plugin, I do not want to use CollectionService because this could possibly interfere with other user’s games. Is there any other way that I can do this?

Thanks, MasterObstacles

Place them all in a table maybe

2 Likes

Uhh, what are you trying to accomplish? I’m a bit confused, sorry, what buttons do you mean?

You should try to use a loop that listens for a click on each button

Assuming you mean TextButtons

for i, v in pairs(allyourbuttons:GetChildren()) do
    v.MouseButton1Click:Connect(function()
        print(v.Name .. " has been pressed")
    end)
end
2 Likes

Sorry for the confusion, I am talking about text buttons and I need all of them to do the same thing

Yes I am talking about TextButtons, sorry for making it unclear. This would work and I completely forgot to say this in my post, but I am going to also be adding buttons (as in using :Clone()) so I don’t think this will work for that. Do you happen to know how I could use the same script but whenever I add a button to the table it still works?

I have a write up about a similar problem here:

It may be useful to you.

1 Like

This may actually be useful. Originally, I did not want to use CollectionService because I am making a plugin and I didn’t want it to interfere with games that use CollectionService. Although, after some testing it seems that plugins using CollectionService don’t actually interfere with the game using CollectionService. I’m going to need to do some extra testing to be sure before I mark this as the solution though. If anybody happens to know and can give me a direct answer that would be great.

Happy aniversary btw :tada:

Basically connecting every textbutton with a MouseButton1Click by calling the function with the parameter as the textbutton.

If this doesnt work, try to do something similar

Untested code:


function listen(obj)
    obj.MouseButton1Click:Connect(function()
        print(obj.Name .. " has been pressed")
    end)
end

for i, v in pairs(allyourbuttons:GetChildren()) do
    if v:IsA("TextButton") then
        listen(v)
    end
end

allyourbuttons.ChildAdded:Connect(function(child)
    if child:IsA("TextButton") then
        listen(child)
    end
end)
1 Like

I wasn’t planning on CollectionService to be the answer… But it is! Thanks for the link it really helped out!