I do this a lot whenever I'm making a main menu. It saves space and makes debugging a lot easier if you do it cleanly.
To accomplish this, we can hold all the buttons in a central place (i.e. in a ScreenGui
) and we can loop through them. Then, we can connect an event to them. Here's an example.
01 | local Players = game:GetService( "Players" ) |
02 | local player = Players.LocalPlayer |
03 | local PlayerGui = player:WaitForChild( "PlayerGui" ) |
04 | local MainMenu = PlayerGui:WaitForChild( "MainMenu" ) |
06 | for _,v in pairs (MainMenu:GetChildren()) do |
07 | if v:IsA( "TextButton" ) then |
08 | v.MouseButton 1 Click:Connect( function () |
09 | print (v.Name .. " was clicked!" ) |
The usage will obviously vary depending on your situation. You didn't give much detail, so I can't include much more in this answer. Good luck!
Accept and upvote if this helps!