:connect is not a straightforward use of functions -- it isn't using functions like they normally are.
This is a really good question -- this is a weird bit of code, and it's a little disappointing not many learning scripters realize how strange it looks.
I'm going to use the Touched event as in my examples, but it works the same way as the MouseClick event.
First, let's write our connection separately from the definition of the function:
function makeRed( part )
part.BrickColor = BrickColor.red()
end
workspace.Wall.Touched:connect( makeRed )
part is defined, in the scope of the makeRed function, to be the first value that makeRed is given when called.
First of all, ignoring the :connect line, we can just use makeRed ourselves:
makeRed(workspace.Player1.Torso)
makeRed(workspace.BasePlate)
The first line, part will be the player's Torso; the second line, it will be the BasePlate.
Here's something Lua lets us do. It's called first class functions or higher ordered functions (EDIT: had serious nomenclature typo)
function makeRed( part )
part.BrickColor = BrickColor.red()
end
function makeBlue( part )
part.BrickColor = BrickColor.blue()
end
local changer
if math.random(2) == 1 then
changer = makeRed
else
changer = makeBlue
end
changer( workspace.BasePlate )
What happened here? changer now stores the value of a function -- that means when we call changer, it will actually be calling the function we assigned it to, which was either makeRed or makeBlue.
:connect does a similar thing. When you say :connect( makeRed ), you aren't calling makeRed. You're giving the value of makeRed so that ROBLOX can call it later.
Here's a fake implementation of how .Touched:connect( x ) might work:
function connect( xfun )
while wait() do
local near = getNearestPart( workspace.Wall )
if areTouching(near, workspace.Wall) then
xfun( near )
end
end
end
How does it know what [the parameter's value] is?
Somewhere, :connect is calling your function with a parameter -- :connect tells your function what the parameter is, in the same way that functions normally get their parameters
Higher ordered functions are awesome. Here's an aside on the makeBlue and makeRed we had earlier:
function makeColorChanger( str )
return function(part)
part.BrickColor = BrickColor.new(str)
end
end
makeRed = makeColorChanger("Bright red")
makeBlue = makeColorChange("Bright blue")
makeGreen = makeColorChange("Bright green")
makeRed( workspace.BasePlate )
makeColorChanger("Bright red")(workspace.BasePlate)
MouseClick Case
The parameter is the player who clicked. (It isn't the LocalPlayer -- a "Script" object (as opposed to a LocalScript) doesn't have the concept of a LocalPlayer)
ROBLOX obviously can know who clicked it, since the game has to know it was clicked at all (so it just also adds, "it was this guy"). So ROBLOX just informs the event of that information.
4 comments
I understand your answer and how variables can store a function as a value, but as for how that snippit of code I posted works it seems that your answer is just "somwhere somehow it's getting the Localplayer?" (I'm not meaning this as an insult or anything, but is there anyway you can explain [if you know how] it is getting that?)
It isn't the LocalPlayer -- it's the player who clicked. ROBLOX obviously knows who clicked it, since the server has to know it was clicked at all. So ROBLOX just informs the event of that information.
(Sorry about the very late response. I didn't realize you added another comment.) So the Event is giving that information to the function? I kind of expanded on this here: https://scriptinghelpers.org/questions/15736/do-events-provide-built-in-arguments
Correct. The event passes the argument to the function.