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

Can someone explain these odd functions?

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I've noticed some people use an odd looking function, different from what I've learned. Here's an example of such a function:

game.Players.PlayerAdded:connect(function(plyr)
-- Code
end)

Can someone explain this to me, please?

3 answers

Log in to vote
3
Answered by
Lupine 60
10 years ago

Very good question indeed.

The traditional function that you're used to will look something like this:

function FunctionName(plyr)
-- Code
end

game.Players.PlayerAdded:connect(FunctionName)

These are used for quite a few reasons -- calling them from other scripts or parts of code, for example.

However, they're mildly bulky and if we don't want to call on them for any other reason, we can do what you posted.

game.Players.PlayerAdded:connect(function(plyr)
-- Code
end)

You'll notice a few things different. We'll start with the top line. The top line is, in essence, your last line (or connection line) that you're used to -- we have our object path and our event, then we're connecting it. However, instead of putting the function name of the function we're connecting this event to, we're making the function then and there.

The last 'end)' you see there is very important -- if you notice on the first line, we have a closed parentheses missing. This is because we need to show our 'connection line' where the bounds of our function is, so we put it all in a very big open/close parentheses, which is closed by the ')' at the end of our 'end'.

Other than that, it works like any other function (aside from the fact that it doesn't have a name, so you can't call it from anywhere else)

Hope this helped, and contact me on Roblox if you have any further, in depth questions.

Ad
Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

It's called an anonymous function.

In Lua, functions are actually a type of variable, like a number or a string. The connect method (a function that is also a member of some object) takes a function variable as an argument.

You're probably used to seeing code like this:

function Touched(hit)
--code
end
script.Parent.Touched:connect(Touched)

Line 1 is syntactic sugar for this:

Touched = function(hit)

Which is the normal way to declare a variable of any other type than a function. Looking at line 4 of the first code block above, you see that I invoke the connect method of the Touched Event of script.Parent, and pass in the function as an argument.

What anonymous functions do is utilize substitution to put the function declaration and connection lines into one line.

This is simpler example of substitution:

a = 4
b = 7
c = a + b
print(a .. " + " .. b .. " = " .. c) -- 4 + 7 = 11

As you can see, the variables a and b represent integer values. The variable c adds the values of a and b by substituting their values in place of their variable names.

Anonymous functions do exactly this, put with functions instead of integers.

Log in to vote
-2
Answered by
Nickoakz 231 Moderation Voter
10 years ago
game.Players.PlayerAdded:connect(function(plyr)
-- Code
end)

When a player is added this function would instantly be called. The player variable is plyr as you set in function(''') So you can make a kick, ban, onchat, connections, or anything when a player joins.

Answer this question