How do you name a function? I'm not sure how to do this. Just messing around with scripting and i'm wondering.
How do you name a function?
2 answers
Well, Yellow didn't exactly finish it.
function derp()
--code
end
script.Parent.Touched:connect(derp)
-- Just an example of an event.
or you could do
script.Parent.Touched:connect(function()
--code|example of an event ^
end)
This one doesn't need the event on the bottom, it's faster really.
function name()
--code
end
Pretty straight forward. The thing after the word function is the name.
5 comments
Or you could always do the other way: name = function() end
But, PiggyJingles, that is different as that is assigning a variable to a function, not assigning a function, which means that the function is not available until runtime, whereas if you don't use a variable you can define a function at line 100 and still use it at line 6.
Name = function() end is not indigenous to Lua, but it is in other programming languages.
@Articulating, you actually cannot do that. function name() is syntactic sugar for name = function() -- They're exactly the same, and you have to define the function body before calling it for both syntaxes.
Ah. I was under the impression that this was the case for Lua. Clearly not.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Anonymous functions are actually slower than explicitly named functions connected to events, due to the fact the function is 'recreated' every time the event fires. It's negblible, but definitely not faster of all things.
Faster as in easier to write for me.