“How do I Pass Variables Between Functions?” I have done lots of research on this topic and can’t seem to find a valid solution.
Methods follow the rule of inheritance where any top-level variables defined are equally accessible to child methods.
Here’s an example:
function foo(x,y) local function bar(y) print(y) end bar(y) end print(foo(1,2)) — 2
Other resources that go more in depth:
Hope this helps!
Please could you be more specif about what you are asking because I am a little confused with what exactly you are asking. Please answer the questions in the scripting support category guidelines in as much depth as possible before posting because it helps a lot: About the Scripting Support category
Passing variables(arguments) into functions
You can pass arguments into functions as parameters. Lets say you wanted to pass two numbers into the function all you would do is put the two number in the parentheses when you call the function. Here is a little code sample:
local function Add(Number1, Number2) print(Number1 + Number2) end Add(20, 30) -- The two numbers you want added together
‘Sharing’ variables between functions
For this all you need to do is put the variable outside the functions and then all the functions will be able to access it. Here is an example:
local Variable = "This is a string" local function foo() print(Variable) end local function bar() print(Variable) end
Returning variables from functions
With the use of return you can return values within the function you just called so that they can be used elsewhere in the script. Here is a little code sample:
local function foo() local Variable = "This is a string" return Variable end local bar = foo() print(bar) -- prints the value of the variable returned from the function.
With the returned value you could then pass that into another function as shown below:
local function foo() local Variable = "This is a string" return Variable end local function bar(Variable) print(Variable) end bar(foo())
It Worked!!! Ty!!!