Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you name a function?

How do you name a function? I'm not sure how to do this. Just messing around with scripting and i'm wondering.

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.

2 comments

0

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.

0

Faster as in easier to write for me.

function name()
	--code
end

Pretty straight forward. The thing after the word function is the name.

5 comments

0

Or you could always do the other way: name = function() end

0

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.

0

Name = function() end is not indigenous to Lua, but it is in other programming languages.

1

@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.

0

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.