What I am trying to do is listen for MouseButton1Down from group of TextButtons.
Assuming you want the same callback for each MouseButton1Down
listener, simply structure your code such that you can traverse a table of TextButton objects. For example:
01 | -- list of buttons |
02 | local buttons = { button 1 , button 2 , button 3 , ... } |
03 |
04 | -- general case callback for MouseButton1Down |
05 | local function onMouseButton 1 Down() |
06 | print ( "The button was clicked!" ) |
07 | end |
08 |
09 | -- traverse the buttons and listen for each MouseButton1Down event |
10 | for _, button in pairs (buttons) do |
11 | button.MouseButton 1 Down:Connect(onMouseButton 1 Down) |
12 | end |
This will solve the general case -- that is -- the scenario where each button is doing relatively the same thing (hence, we're using onMouseButton1Down
directly as the callback to Connect
).
Instead of the general case, you may also want a special case. Here, we can make each callback to the listener a little bit more unique to the button that fired the event. For example:
01 | -- list of buttons |
02 | local buttons = { button 1 , button 2 , button 3 , ... } |
03 |
04 | -- specific case callback for MouseButton1Down |
05 | local function onMouseButton 1 Down(button) |
06 | print ( "The button: " .. button.Name .. " was clicked!" ) |
07 | end |
08 |
09 | -- traverse the buttons and listen for each MouseButton1Down event |
10 | for _, button in pairs (buttons) do |
11 | button.MouseButton 1 Down:Connect( function () |
12 | onMouseButton 1 Down(button) -- pass the button that was clicked to the callback |
13 | end ) |
14 | end |
In this case, we can use the same onMouseButton1Down
callback for every button, while also receiving some information about what button fired the event.
Technically I'd still consider this a general case, since we get to re-use most of the code here. However, we are passing a different "anonymous" function to Connect
every iteration, so that's where I derived "special case" from.
I hope this solved your problem. If you have any questions, let me know.
I think I understand what you're asking, but let me reiterate just to make sure. If you have two sets (A and B) of buttons, where each set has a different callback to MouseButton1Down, then you would like to switch the place of the button that was clicked from set A into set B (so that the button now uses the callback for set B.)
Yeah, I wouldn't do that. Instead, I'd keep a single callback and just implement some debounce mechanic. For example:
01 | local buttons = { button 1 , button 2 , button 3 , ... } |
02 | local buttonStates = { } -- storage of button states to keep track of what's happening to each button (also useful for debounce mechanics) |
03 |
04 | local function onMouseButton 1 Down(button) |
05 | local buttonData = buttonStates [ button ] -- get the button data |
06 |
07 | -- example of other possible use (keep track of times clicked) |
08 | buttonData.timesClicked = buttonData.timesClicked + 1 |
09 |
10 | -- example of debounce |
11 | if (buttonData.enabled) then |
12 | -- execute code when button is enabled |
13 | print ( "The button: " .. button.Name .. " was clicked!" ) |
14 | else |
15 | -- execute code when button is disabled |
Well you can certainly do this:
Using a Table
01 | local button 1 = script.Parent.TextButton 1 |
02 | local button 2 = script.Parent.TextButton 2 |
03 |
04 | local array = { button 1 , button 2 } |
05 |
06 | local function Fired() |
07 | print ( "Text Button Activated" ) |
08 | end |
09 |
10 | for _, button in pairs (array) do |
11 | button.Activated:Connect(Fired) |
12 | end |
Alternative
1 | local button 1 = script.Parent.TextButton 1 |
2 | local button 2 = script.Parent.TextButton 2 |
3 |
4 | function Fired() |
5 | print ( "Text Button Activated" ) |
6 | end |
7 |
8 | button 1. Activated:Connect(Fired) |
9 | button 2. Activated:Connect(Fired) |
As a note, I used Activated
rather than MouseButton1Down
since Activated
works for all devices, not just computers.
Localized variables allows you to move the items, while only editing the top lines, in case you need to call the TextButtons within a function of the script.
For Multiple Tables, you could try having two Local Scripts and enabling one when the other fires, like so:
Local Script 1
01 | -- Variables |
02 | local button 1 = script.Parent.TextButton 1 |
03 | local button 2 = script.Parent.TextButton 2 |
04 |
05 | local array 1 = { button 1 } |
06 | local array 2 = { button 2 } |
07 |
08 | local other = script.Parent.LocalScript 2 |
09 |
10 | -- Functions |
11 | local function Fired 1 () |
12 | print ( "Text Button Activated" ) |
13 | other.Disabled = false |
14 | script.Disabled = true |
15 | end |
Local Script 2
01 | -- Variables |
02 | local button 1 = script.Parent.TextButton 1 |
03 | local button 2 = script.Parent.TextButton 2 |
04 |
05 | local array 1 = { button 1 } |
06 | local array 2 = { button 2 } |
07 |
08 | local other = script.Parent.LocalScript 1 |
09 |
10 | -- Functions |
11 | local function Fired 1 () |
12 | print ( "Text Button Activated" ) |
13 | other.Disabled = false |
14 | script.Disabled = true |
15 | end |