So....I have 25 text buttons for my inventory system, meaning...1 function for each button if someone wants to equip something...that's 25 different functions, JUST FOR EQUIPPING, not even for clicking Rightmousebutton. So is there a way around this? at all without having to type 50 functions?
✓ ANSWER ACCEPTED
I have 25 textbuttons, how can I make it so that I don't have to type 25 different functions?
1 answer
✓ Accepted answer
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.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local MainMenu = PlayerGui:WaitForChild("MainMenu") --you could just have the local script inside the main menu, but whatever
for _,v in pairs(MainMenu:GetChildren()) do
if v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
print(v.Name .. " was clicked!")
end)
end
end)
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!
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
learn for loops
You can use numeric (I would use) or generic for loop for this