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

How can I pass additional arguments through :connect?

Asked by 6 years ago

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

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

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)
1
this is cleanest with anonymous functions, i.e., game.Players.PlayerAdded:connect(function(p) p.Chatted:connect(function(msg) end) end) Perci1 4988 — 6y
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. chess123mate 5873 — 6y
0
The new event connection is being defined, yes. But the function is constant in the second code :) Goulstem 8144 — 6y
0
Whereas in the first code, the function would have to be defined when the player is added. Goulstem 8144 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

Answer this question