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

How to pass values into an event?

Asked by 6 years ago
function test(var)
    print(var)
end

test("hello")

will print "hello"

How do I pass a value into an event e.g.

function test(p,var)
    print(var)
end

game.Players.PlayerAdded:Connect(test("hello"))

returns error

21:17:26.792 - Attempt to connect failed: Passed value is not a function
21:17:26.793 - Stack Begin
21:17:26.793 - Script 'Workspace.Script', Line 5
21:17:26.794 - Stack End
21:17:26.794 - attempt to call a nil value

Is it possible to pass values when calling a function like with the first example but with an event?

Thank you in advance to anyone that helps. If anything is unclear just leave a comment and I will try to explain.

0
game.Players.PlayerAdded:Connect(function() end                                                                  (Line 5) While you did game.Players.PlayerAdded(test("hello"))  mudathir2007 157 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

your issue here that the argument passed for Connect has to be a function. What you are doing is passing nothing. You are actually calling the test function and getting it to print before it even connects to PlayerAdded.

The fix for this is to make an entirely new function that will, in turn, fire your test function

function test(p,var)
    print(var)
end

game.Players.PlayerAdded:Connect(function(player)
    test(player, "hello")
end)

Notice how instead of calling the function (which would return nothing for the connection), I made an entirely new function that handles the connection and the print separately

Hope this helped!

0
Thank you! 123marble 61 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

game.Players.PlayerAdded:Connect(function() test("Arguments here") end)

Answer this question