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

What's the Difference between "function Event()" and "Event=function()"?

I've seen some scripts with Variable=function()what difference does it have between function Name()?

6 comments

2

There isn't really any difference, however, setting the function up as a Variable in a Table allows you to use it in, well, a table (Example: local Table = {Var = function() print("Yay!") end)}), and you can call it still (Example: Table.Var()).

0

ohh...So basically it both can be used?

1

Yes, it's just that the Variable version has a few more advantages, but can sometimes error when not used correctly.

1

It's just the style that people write code in.

0

ikr

0

function Name() is the preferred method i believe, so use it whenever possible.

1 answer

✓ Accepted answer

Your question has been adequately answered already, but to remove it from the Unanswered sort category, I'm going to a write a full answer.

There is absolutely no difference between:

function funcName() end

and:

funcName = function() end

In fact, the former is actually just syntactic sugar for the latter!

When using Tables, either form works except for once case:

local tab = {}

function tab.funcName() end --valid
tab.funcName2 = function() end --valid
tab["funcName3"] = function() end --valid (Also lets you make functions with non-string identifiers)

function tab["funcName4"]() end --INVALID
	--This is the only case where the sugar doesn't work. I'm not sure *why* it doesn't work, but I've tested it before and it provably does not work.

1 comment

0

Cool! Thanks adark!

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