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

MyFunction = function() OR function MyFunction() ?

(sorry for confusing title) Code Number 1:

local PrintSomething = function(text)
	print(text)
end

Code Number 2:

local function PrintSomething(text)
	print(text)
end

what's the difference between Code 1 and Code 2? wich one should i use? will it increase or decrease performance?

3 comments

0

They're both the same, I can't exactly explain when to use them differently, i would leave that to someone else, I'll just say local PrintSomething would be used in a moudle script, and lcoal function would restrict the use of the function to anything above

0

use either one, they are the same

0

A simple search would have sufficed. Here is a popular post just about it. https://scriptinghelpers.org/questions/59764/what-does-all-these-different-types-of-functions-do

1 answer

✓ Accepted answer
function func(...)
    --...
end

is syntax sugar (a nicer way of writing something else) for this:

func = function(...)
    --...
end

The syntax sugar for a local function is this:

local func
func = function(...)
    -- func available as an upvalue here
end

And not this:

local func = function(...)
    -- func is not available as an upvalue 
end

That is because this example will not make func an upvalue (external local variable) inside the function's body, and because of that you won't be able to make recursive function calls.

But to answer your question, it really doesn't matter which you use. local function func(...) end is the same as local func; func = function(...) end and function func(...) end is the same as func = function(...) end.

This discussion is preserved as it was. New answers, comments, and votes are closed.