Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I have 25 textbuttons, how can I make it so that I don't have to type 25 different functions?

Asked by
Galicate 106
5 years ago

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?

1
learn for loops SaltyPotter 362 — 5y
1
You can use numeric (I would use) or generic for loop for this SaltyPotter 362 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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!

Ad

Answer this question