Some way to simplify multiple MouseClick connections?

I need to add a number to a variable depending on the button I stepped on but as a result I have what you see in the image and you want to simplify it a bit but I don’t know how.

and if the code is in Spanish since it is my native language

You can use the collection service to tag all the buttons. Then loop through the tagged and assign each object a function.

local cs = game:GetService("CollectionService")
local buttons = cs:GetTagged("Button")

for i, v in pairs(buttons) do

    v.MouseButton1Click:Connect(function()

        --code

    end)

end

To actually tag, you can either do it manually or using a script:

for i, v in pairs(screengui:GetDescendants()) do

    --check if text button and has "CertainButton" within the name somewhere

    if v:IsA("TextButton") and v.Name:find("CertainButton") then 

         cs:AddTag(v, "Button")

    end

end
2 Likes

Thank you! it took me a while to understand but with some modifications I made it work

1 Like