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

How can I pass additional arguments through :connect?

I want to pass the user through a chatted event without anonymous functions.

2 answers

✓ Accepted answer

If you define the function to be used for the Chatted event inside of the function to be used for the PlayerAdded event, the parameter for the Player will be inherited into the Chatted function.

function add(p) --function for PlayerAdded
	local function whenChat(m) --function for Chatted
		print(p.Name.." chatted: "..m); --'p' variable inherited
	end
	p.Chatted:Connect(whenChat) --connection for Chatted
end

game.Players.PlayerAdded:Connect(add)

Although, I do suggest simply using anonymous functions because - one, the code above is defining the function for the Chatted event every time a player joins. And two, you won't need to worry about networking your variables.

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		print(plr.Name.." chatted: "..msg);
	end)
end)

4 comments

1

this is cleanest with anonymous functions, i.e., game.Players.PlayerAdded:connect(function(p) p.Chatted:connect(function(msg) end) end)

1

Minor: you say "the code above defines the function...every time a player joins", but you're doing that anyway on line 2 (in your 2nd script block) -- it is "remade" every time with a new "plr" in its scope.

0

The new event connection is being defined, yes. But the function is constant in the second code :)

0

Whereas in the first code, the function would have to be defined when the player is added.

It depends on what the script is, you cant put connect in random places..

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