Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can one create an event handler that listens for a multitude of the same events?

Asked by 6 years ago
Edited 6 years ago

What I am trying to do is listen for MouseButton1Down from group of TextButtons.

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

General Case

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
02local buttons = {button1, button2, button3, ...}
03 
04-- general case callback for MouseButton1Down
05local function onMouseButton1Down()
06    print("The button was clicked!")
07end
08 
09-- traverse the buttons and listen for each MouseButton1Down event
10for _, button in pairs(buttons) do
11    button.MouseButton1Down:Connect(onMouseButton1Down)
12end

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).

Special Case

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
02local buttons = {button1, button2, button3, ...}
03 
04-- specific case callback for MouseButton1Down
05local function onMouseButton1Down(button)
06    print("The button: " .. button.Name .. " was clicked!")
07end
08 
09-- traverse the buttons and listen for each MouseButton1Down event
10for _, button in pairs(buttons) do
11    button.MouseButton1Down:Connect(function()
12        onMouseButton1Down(button) -- pass the button that was clicked to the callback
13    end)
14end

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.

Comment Question #1

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:

01local buttons = {button1, button2, button3, ...}
02local buttonStates = {} -- storage of button states to keep track of what's happening to each button (also useful for debounce mechanics)
03 
04local function onMouseButton1Down(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
View all 34 lines...
0
Now what if, I had two groups of buttons and I wanted them change change to the opposite group when clicked? Would I update the button array accordingly? Stephenthefox 94 — 6y
0
For this example, that wouldn't work. However, I wouldn't recommend doing something like that anyway. If you're just going to toggle the functionality of the button press, I would recommend implementing a sort of debounce in the callback. ScriptGuider 5640 — 6y
0
I'll edit my answer to elaborate on that. ScriptGuider 5640 — 6y
0
Makes since, thank you. Stephenthefox 94 — 6y
0
No problem. I recommend checking out the edit anyway, I think that will help a lot. ScriptGuider 5640 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Well you can certainly do this:

Using a Table

01local button1 = script.Parent.TextButton1
02local button2 = script.Parent.TextButton2
03 
04local array = {button1, button2}
05 
06local function Fired()
07    print("Text Button Activated")
08end
09 
10for _, button in pairs(array) do
11    button.Activated:Connect(Fired)
12end

Alternative

1local button1 = script.Parent.TextButton1
2local button2 = script.Parent.TextButton2
3 
4function Fired()
5    print("Text Button Activated")
6end
7 
8button1.Activated:Connect(Fired)
9button2.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
02local button1 = script.Parent.TextButton1
03local button2 = script.Parent.TextButton2
04 
05local array1 = {button1}
06local array2 = {button2}
07 
08local other = script.Parent.LocalScript2
09 
10-- Functions
11local function Fired1()
12    print("Text Button Activated")
13    other.Disabled = false
14    script.Disabled = true
15end
View all 30 lines...

Local Script 2

01-- Variables
02local button1 = script.Parent.TextButton1
03local button2 = script.Parent.TextButton2
04 
05local array1 = {button1}
06local array2 = {button2}
07 
08local other = script.Parent.LocalScript1
09 
10-- Functions
11local function Fired1()
12    print("Text Button Activated")
13    other.Disabled = false
14    script.Disabled = true
15end
View all 30 lines...

Answer this question