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

Is this the correct way to define a _G. Function?

I'm experimenting with a type of variable/function that is new to me (_G.)

I tried defining a function like this:

_G.function MyFunction() 
--code here
end)

However the script editor gave me a red line, so I changed it to this:

_G.MyFunction = function()
--code here
end)

For my future reference, is the second way the correct way to define _G. functions?

1 answer

✓ Accepted answer

When defining functions in any table (_G is a table too), you can do it in two main ways; either like this:

_G.MyFunction = function()
--code here
end)

or like this:

function _G.MyFunction()
-- code here
end)

While this is not related to your specific case of _G, you can also do this:

function MyTable:MyFunction()
-- code here
end)

This will define the function in such a way that the first argument will always be refered to as self, and if you later call the function like MyTable:MyFunction() then the first argument will be the table itself. This can be used for OOP in Lua.

2 comments

0

Thanks!

0

I would not consider lua OO without classes or structures

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