I also don’t like how devhub uses the custom function method every time, so I get really confused, but then I scroll to the bottom and find out that it’s a custom function.
^^, basically let’s say you need to run a function each time a head shows up in workspace, first you would have to run through all already existing heads in workspace and run this function, then create a event for newly made ones. So basically you have this function if that’s the case. And if you want to change something… Well it becomes a drag, also makes your code look neater.
This is actually really great practice if you’re doing a large scale project or plan to use something more than once. Functions is a great way to keep your code a lot less cluttered and a lot more organized. It might seem useless if your only using it once but if you plan on using it multiple times or even just use it for simplicity you’ll notice that your code will most likely end up being smaller.
As an example I made a OTS camera script and created a Enable() and Disable() function so now if I want to enable the OTS camera I can simply type Enable() and saves a ton of lines, and saves a lot of scrolling time. This is great for multiple developers to be able to read your code.
It improves reusability and readability. Oftentimes, anonymous functions contribute to large and unreadable function blocks. Generally speaking, the smaller your logical blocks of code are, the easier it will be to understand and maintain.
local Players = game:GetService("Players")
function PlayerAdded(Player)
print(Player.Name)
end
Players.PlayerAdded:Connect(PlayerAdded)
for _, Player in ipairs(Players:GetPlayers()) do
PlayerAdded(Player)
end
Occasionally, albeit very rarely, you want to connect a function to an event but also have the control of running it whenever you please. That’s when you would define a function as a variable rather than use an anonymous one.