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

How do you connect to a function with an argument?

Asked by 7 years ago

Sorry if this is something really easy to learn, but I just don't get it. I'm trying to connect to my function while passing along the argument for num1.

01wait()
02player = game.Players.LocalPlayer
03MainGUI = script.Parent.ScreenGui
04Scroll = MainGUI.ScrollingFrame
05RStorage = game:GetService("ReplicatedStorage")
06ClassFolder = RStorage.ClassFolder
07 
08-- Button List
09Class1 = Scroll.Class1
10Class1Event = ClassFolder.Class1Event
11 
12 
13function mainEvent(num1)
14    if num1 == 1 then
15        Class1Event:FireServer()
16    end
17end
18 
19Class1.MouseButton1Down:connect(mainEvent(1))

It gives me the error: "Attempt to connect failed: Passed value is not a function" and afterwards "attempt to call a nil value"

The error is line 19

0
Why would you want an argument which will be the same forever? Just use variables! hiimgoodpack 2009 — 7y
0
I'm pretty new at scripting, how would you do this? Explosivesguy2 20 — 7y
0
Re-read my answer btw User#18043 95 — 7y

2 answers

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

You cannot pass a value through the connection. You have to call the function with a parameter sent in through the parenthesis to send the value.

01MainGUI = script.Parent.ScreenGui
02Scroll = MainGUI.ScrollingFrame
03Class1 = Scroll.Class1
04 
05function mainEvent(num)
06    if num1 == 1 then
07        Class1Event:FireServer()
08    end
09end
10 
11function passparameter()
12    mainEvent(1)
13end
14 
15Class1.MouseButton1Down:Connect(passparameter)

Also :connect() is deprecated, ROBLOX is case-sensitive so it has to be :Connect()

0
I forgot the :FireServer() doesn't require a parameter, only :FireClient() does. Sorry for my mistake. User#18043 95 — 7y
0
it's ok lol, I didn't use the passparameter Explosivesguy2 20 — 7y
0
Sociallycole, it does not matter on connect's capitalization. roblox simply ignores this. Albino_Albino 0 — 7y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

As @User#18043 mentioned, you can't directly pass arguments, but you can still do this:

Class1.MouseButton1Down:Connect(function() mainEvent(1) end)

If you're going to make multiple connections to MouseButton1Down on multiple, different Classes and want to utilize the same mainEvent function, this would be a solution as you can supply different arguments, such as in the case of GUI TextButtons:

1local buttons = YouGui
2 
3buttons.FirstButton.MouseButton1Down:Connect(function() mainEvent(1) end)
4buttons.SecondButton.MouseButton1Down:Connect(function() mainEvent(2) end)

Answer this question