Local/Global Variables

function onLoad() globalString = "I will always exist." doATestPrint() end function doATestPrint() print(globalString) --prints the string successfully end

function onLoad() local localString = "I don't exist outside of this function." doATestPrint() end function doATestPrint() print(localString) --Will not print the string --Because that string doesn't exist in this function, only in onLoad end

function onLoad() local localString = "I will be passed to another function." doATestPrint(localString) end function doATestPrint(passedString) print(passedString) --Will print successfully end

So far we have only worked with global variables, ones that persist and are available to the entire code. So for example, this would create a global variable.But for most applications, using a local variable is preferred. They are created by placing the word "local" before your variable name when the variable is being created. Local variables only exist within the function they are created. When the function ends, a local variable is forgotten. If you only use Global variables, in longer scripts you will find yourself thinking of more and more complicated variable names, accidentally overwriting variables, accidentally using old variables, etc. So below is an example of how this local variable would be different.But as you move your code into different functions, they will need a way to communicate and share these local variables sometimes. If that is the case, you can share a local variable with another function by sending it as a parameter.I personally avoid general variables as much as possible. I only ever use them if I need data to persist. For example, if you reference the example table, the "Save/Load" memory example has a Global variable which tracks the value of a counter.