Hey @Robenhood1416! I believe I know what you are getting at, however I think you may have a few terms mixed up so I will attempt to clarify and provide a solution.
First, I’ll go over the example of a single button’s Activated event connected to a single function:
--The named function approach
function onClick()
print("Ouch, I was clicked")
end
[Button].Actiavted:Connect(onClick)
--The anonymous function approach
[Button].Actiavted:Connect(function()
print("Ouch, I was clicked")
end)
The two examples above act exactly the same, however the first used a defined function named onClick
and the second uses an anonymous function. In either case, the event of clicking the button ([Button].Activated
) triggers a single function (either onClick
or the anonymous function in the second example).
Every button has an event which you can connect a single function to using the :Connect()
method of the event. (I’m assuming you’re using something like a TextButton or ImageButton here, but the concept still applies if you’re using something else too).
Often there are situation where multiple events should trigger the same function - although we may want the function to act slightly differently depending on the event that triggered it. Below is an example of how you might connect multiple buttons’ Actiavted
event to the same function:
(This example assumes there are 3 buttons named “A”, “B”, and “C” inside a ScreenGui named “ScreenGui”)
local player = game.Players.LocalPlayer
local screenGui = player.PlayerGui.ScreenGui
function clicked(whichButton)
if whichButton == screenGui.A then
--The "A" button was clicked
elseif whichButton == screenGui.B then
--The "B" button was clicked
elseif whichButton == screenGui.C then
--The "C" button was clicked
end
end
for _, button in ipairs({screenGui.A, screenGui.B, screenGui.C}) do
--With each pass of this loop, `button` is a reference to the TextButton we
-- are connecting the `Activated` event to
button.Activated:Connect(function()
clicked(button)
end)
end
In this example the Activated
event of each button (A, B, and C) is connected to a unique anonymous function which calls the clicked
function passing as an argument a reference to the button that was clicked. The clicked
function can then act differently depening on which button was clicked by comparing the argument it was passed.
@WheezWasTaken provided a similar and good answer on how to incorporate this into a RemoteEvent to let the server know which team the player selected, but I hope this post helped clarify how to connect multiple events to the same function.