Avoiding thousands of UI Events

Basically my problem is that I have to script and UI that contains let’s say… 10 buttons, and I want to link those 10 buttons in just one local script, so I have to do this:

UIname1.MouseButton1Click:Connect(function()
   print("1")
end)

UIname2.MouseButton1Click:Connect(function()
   print("2")
end)

UIname3.MouseButton1Click:Connect(function()
   print("3")
end)

-- Etc...

Is there any way to avoid copy pasting these functions?

I think you can make a local function to make it easier to do if your UI’s do simliar things.

This is what you would do

local function GuiName() 
 -- Put whatever you want to happen to you gui 
end
GuiName()-- the way you call the function

This is how you would do it if every code was the same for each button, if they have different things like changing the name then don’t add that to the function just add that manually.

If they all have the same parent, you can just iterate through the children (using a for loop), check if it’s a textbutton and then connect the function to the Activated/MouseButton1Click event.
Not sure if I misunderstood the question though

1 Like