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.
01 | wait() |
02 | player = game.Players.LocalPlayer |
03 | MainGUI = script.Parent.ScreenGui |
04 | Scroll = MainGUI.ScrollingFrame |
05 | RStorage = game:GetService( "ReplicatedStorage" ) |
06 | ClassFolder = RStorage.ClassFolder |
07 |
08 | -- Button List |
09 | Class 1 = Scroll.Class 1 |
10 | Class 1 Event = ClassFolder.Class 1 Event |
11 |
12 |
13 | function mainEvent(num 1 ) |
14 | if num 1 = = 1 then |
15 | Class 1 Event:FireServer() |
16 | end |
17 | end |
18 |
19 | Class 1. MouseButton 1 Down: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
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.
01 | MainGUI = script.Parent.ScreenGui |
02 | Scroll = MainGUI.ScrollingFrame |
03 | Class 1 = Scroll.Class 1 |
04 |
05 | function mainEvent(num) |
06 | if num 1 = = 1 then |
07 | Class 1 Event:FireServer() |
08 | end |
09 | end |
10 |
11 | function passparameter() |
12 | mainEvent( 1 ) |
13 | end |
14 |
15 | Class 1. MouseButton 1 Down:Connect(passparameter) |
Also :connect()
is deprecated, ROBLOX is case-sensitive so it has to be :Connect()
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:
1 | local buttons = YouGui |
2 |
3 | buttons.FirstButton.MouseButton 1 Down:Connect( function () mainEvent( 1 ) end ) |
4 | buttons.SecondButton.MouseButton 1 Down:Connect( function () mainEvent( 2 ) end ) |