I have a surface GUI with 11 buttons, each with a different name. When one of the buttons is clicked, I want it to fire a BindableEvent and store what button was pressed and the player who pressed it in two variables.
I could use a separate script for each button, but that would take too long and be bad practice. How would I be able to detect when one of the 11 buttons is pressed and know which one got pressed by who?
You can make a for loop to get all the children in a folder (or something).
Let’s say all your buttons are in a Frame, and a LocalScript is in that same Frame where all the buttons are located.
Your code should be:
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("TextButton") and v.Name == "NAME HERE" then
v.Activated:Connect(function() -- when any button thats in the frame was clicked/tapped
-- do your stuff
end)
end
end