Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to pass values into an event?

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.

1 comment

0

game.Players.PlayerAdded:Connect(function() end                                                                  (Line 5) While you did game.Players.PlayerAdded(test("hello")) 

2 answers

✓ Accepted answer

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!

1 comment

0

Thank you!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.