As a bit of an aside:
anonymous functions are useful because they require fewer lines of you to write. Two fewer, specifically. When you 'stack' them:
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
end)
end)
You write code with a typographical layout that exactly matches the logical layout. Basically, it makes it easier to follow along with what's actually being done in the code.
The cost of this is speed: an anonymous function has to be re-declared in memory when it's called. This is because the entire function goes out of scope once it finishes running. For most cases, this isn't an important distinction, but large functions can become laggy for no reason other than this.
On the topic of actually using them, it's whether or not you need to use it repeatedly. Inline code such as this is by nature one-off. You can call it in a loop, or inside another event-connected function, of course, but it still technically only works once.
If, for instance, you had a PlayerAdded event that connects after one or more Players have already joined the game, using a named function will be easier:
function PlayerAdded(player)
player.CharacterAdded:connect(function(character)
end)
end
for _, v in ipairs(game.Players:GetPlayers()) do
PlayerAdded(v)
end
game.Players.PlayerAdded:connect(PlayerAdded)
1 comment
Both answers were good, but I prefer this one, thanks to both!