How to make a function that handles all the buttons i.e Activated, visibilty

How would I accomplish making a function that handles all the buttons and UI’s? I’ve a lot of buttons, Image labels and image buttons and can’t just hard code all of them as there is an more efficient way.

Can’t you use a loop?

for _, button in ipairs(buttons) do
    button.Activated:Connect(function()
        -- etc
    end)
end
1 Like

You could do something like

for i,v in pairs(player.PlayerGui:GetDescendents()) do
   if v:IsA("TextButton") or v:IsA("ImageButton") then
      v.Activated:Connect(function()
         --some kind of code that checks which button you pressed, probably based on name
      end)
   end
end

No I can’t the buttons are for different uses and there like 20 of them.

You can group buttons into arrays that do a similar function then just loop over them:

local ButtonsArray = {Button1,Button2}

for _, Button in ipairs(ButtonsArray) do
    Button.MouseButton1Down:Connect(function()

    end)
end

Also you need to be smart about creating buttons, let say for example your making a shop with a lot of buttons, it would be best if you store all the items in a database and then create the buttons via script.

1 Like

If they have different uses and you have a lot, I recommend checking the name of the button when you loop through them, and do what you want based on that. There isn’t really a way to just magically code them all to work with a short bit, you need to code each one

I don’t know if this will work put all the button inside a folder(In the screen Gui)
Name the folder buttons.

Now type this (Note script in the same screen Gui)

for i, buttons in pairs(script.Parent.buttons:GetChildren) do 
           if button:IsA("TextButton") or button:IsA("ImageButton") then
           button.MouseButton1Up:Connect(function()
      end)
   end
end

@KdudeDev, This is not an effective solution for a lot of buttons, instead if your making a shop gui, for example, then have a one large module or table that holds all the data, then you would create some sort of base button which would be a blueprint for what each button should look like, for example you would make an image button with a text label for the price and the name, and then you would do something like this.

local ShopData = {
    [1] = {Name = "Item1", Price = 20},
    [2] = {Name = "Item2", Price = 50}
}
local ShopGuiFrame
local BaseShopButton

for _, ShoppingData in ipairs(ShopData) do
    local ShoppintDataButton = BaseShopButton:Clone()
    ShoppingDataButton.NameLabel.Text = ShoppingData.Name
    ShoppingDataButton.PriceLabel.Text = ShoppingData.Price
    ShoppingDataButton.Parent = ShopGuiFrame
    --listening for input
    ShoppingDataButton.MouseButton1Down:Connect(function()
   
    end)
end

Generally, whenever your creating a gui with a lot of buttons it should always be done through a script.

You do not really get a more efficient way, it is more as if you’re making more readable code.

Just try your own style while making only one function for each task, no two events should have two differently named or anonymous functions that do exactly the same thing.

He never said it was a shop, he wanted EVERY button in his game.

You could let a table contain all functions that need to run for every object, with the index [object.Name] and function [function() – end], get all these specific objects at one place through CollectionService or something else and pass the respective function in the table as a callback to Obj.Activated:Connect() .

I already achieved it using three stuffs.

  • Tables
  • Server Client Communication
  • OOP