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

How do you name a function?

Asked by 10 years ago

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

Log in to vote
1
Answered by 10 years ago

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.

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. adark 5487 — 10y
0
Faster as in easier to write for me. YellowoTide 1992 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
function name()
    --code
end

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

0
Or you could always do the other way: name = function() end PiggyJingles 358 — 10y
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. Articulating 1335 — 10y
0
Name = function() end is not indigenous to Lua, but it is in other programming languages. GoldenPhysics 474 — 10y
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. adark 5487 — 10y
0
Ah. I was under the impression that this was the case for Lua. Clearly not. Articulating 1335 — 10y

Answer this question