Alright so I know what a function IS...but I also started hearing about these...
Global Function ( _G ) NewProxy
What confuses me...is well I thought that a function was just a function....but apparently there are like a billion...commands??...that fit under the section "function". Do they all do different things? And if so, is this NOT the function template?
function -- the name here () --Stuff I don't understand here xD-- end
I thought this was like the template tht a function follows. Also remember, I thought a "function" was a single thing, not like a billion things lel
That is the function "template", and pretty much all functions follow that kind of structure
[local] function funcName(params) body end
However, funcName
can be something other that just a string.
If it belongs to a table, it will be tableName.funcName
If the table belongs in a table, then it could be rootTable.parentTable.funcName
, and so on.
A function is kind of like a command. You input values and it returns a value.
local myFunc = function(x, y) -- x and y will be inputted later on. return x + y -- Gives the value back to where it will be executed as a regular variable (in this case x + y) end print(myFunc(10, 20)) -- 10 and 20 are represented as x and y in the function. (Functions don't run automatically)
It will print:
30
If you do not understand feel free to ask me in the comments.
That was how a function works on a basic level HOWEVER functions ARE variables, just a different type of variable.
Variables have types: Integer, String (text), Boolean (true or false), Table, Function, and Userdata (Which is kinda multiple types but whatever)
Functions are like a variable meaning there can be multiple functions.
The way a function is declared does not set it apart from other variables
function myFunc(x, y) return x + y end print(myFunc(10, 20))
It will still print:
30
You can declare as many functions or call them as much as you would like
local function myFunc(x, y) -- using local function because we aren't using getfenv() return x + y end local function anotherFunc(x, y, z) -- Can have as many parameters as you wish (you can have 0). return x + y - z end print(myFunc(10, 20)) print(myFunc(20, 5)) local value = anotherFunc(10, 20, 15) -- The returned value can be stored in a variable print(value)
This will print:
30 25 15
There are also built in functions like newproxy
(Which I'm not going to use as an example because it's not simple to use)
Say print
. print
IS a function along with Instance.new
, Vector3.new
, etc..
Doing:
print("Hello world!") local x = Instance.new("Part") -- Instance.new returns a value unlike print
Calls the function print
which does some internal stuff to print "Hello world!"
and calls the functions Instance.new
which creates a new part in this case and then returns it.
Global functions using _G
and shared
should not be used. I would recommend modules (module scripts) instead.