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

How do you put extra parameters in events?

Asked by 5 years ago

I am trying to make a list of buttons that all need to execute the same general thing but with slightly different names, cost, among other data. I'm not sure how to put in extra parameters other than who triggered it without writing separate functions. For example:

1function onclick(player,GuiObject) -- How can you get the GuiObject
2    print(player.. " clicked ".. GuiObject)
3end
4GuiObject.MouseButton1Click:Connect(onclick)

How would I get the GuiObject without writing a function for each event?

1 answer

Log in to vote
0
Answered by 5 years ago

You can use a table with the list of buttons and use a for loop to create events for all of them. For example, you can do something like this

01local player = game.Players.LocalPlayer
02local buttons = {
03    -- insert button1,
04    -- insert button2,
05    -- insert button3,
06    -- insert other buttons,
07}
08 
09function ButtonClicked(button_clicked)
10    print(player.Name .. " clicked " .. button_clicked.Name)
11    if button_clicked == buttons[1] then
12        -- insert code when button 1 pressed
13    elseif button_clicked == buttons[2] then
14        -- insert code when button 2 pressed
15    elseif button_clicked == buttons[3] then
View all 25 lines...
0
Someone had the same question yesterday if you want some more info about it  https://scriptinghelpers.org/questions/90481/how-does-this-function-keep-passing-a-nil-value#83843 royaltoe 5144 — 5y
0
Wait can an in pairs loop connect events all at once? Won't that disable the connection? 123nabilben123 499 — 5y
0
in pairs loop is just a for loop but for feasibility with objects in a table. all of them will have the same event and no, it does not disable the connections. InfinityEngine 223 — 5y
0
Thank you, this really simplifies the code I have to write. awesomebanana2018 36 — 5y
Ad

Answer this question