Thank you very much for these videos, they are helping me a lot. I have a question about the treasure chest script though, in the function you are feeding in amount as a parameter, but coins is used out from outside the function? Does that mean that coins is global for that script. I would have thought that to change coins you would either have to return a value from the function and assign that to coins in the script body, or pass coins as a parameter by reference. Is there something here that I'm missing?
In programming, there's a term called "scope" which defines where a variable or other named value can be used. Since we define our `coins` variable at the highest level of scope, in other words, not within a function or statement, but at a more global level for the script, we can use it anywhere in our script after we've defined it. If we create a variable within a function, it would only have the local scope within that function, meaning it can only be used inside the function itself. Parameters for functions act as variables that are defined this way, inside the function itself. The variable's indentation level is a good indicator of its scope. If it's as far left as possible (no indentation), it should have the highest level of scope and be usable throughout the whole script (after it's defined, of course). If the variable definition is indented and within a statement of some sort, such as a function definition, then it should only have the scope of that statement/function and be unusable outside of that statement/function. I hope that helps clear things up for you!
Ok, for some reason YT won't let me reply to your reply, so new comment it is, with apology. I think what bugged me, and was poorly articulated is that I'm used to Python, and variables in Python script body does not fall under the scope accessible to functions unless you declare them as global, but I see lua handles things differently.
A little. Lua has a global table that stores any global variables able to be used across multiple scripts, with some exceptions, but declaring a variable as a local variable using the `local' keyword sets the scope of the variable to the block it's declared in. If it's declared on it's own, the "block" of code it's scope is limited to is the script itself. If declared in a function or other block within that script, then its scope is obviously limited to that specific block instead. It's a little trickier in Roblox than just a Lua script on general though.
Thank you very much for these videos, they are helping me a lot. I have a question about the treasure chest script though, in the function you are feeding in amount as a parameter, but coins is used out from outside the function? Does that mean that coins is global for that script. I would have thought that to change coins you would either have to return a value from the function and assign that to coins in the script body, or pass coins as a parameter by reference. Is there something here that I'm missing?
In programming, there's a term called "scope" which defines where a variable or other named value can be used. Since we define our `coins` variable at the highest level of scope, in other words, not within a function or statement, but at a more global level for the script, we can use it anywhere in our script after we've defined it.
If we create a variable within a function, it would only have the local scope within that function, meaning it can only be used inside the function itself. Parameters for functions act as variables that are defined this way, inside the function itself.
The variable's indentation level is a good indicator of its scope. If it's as far left as possible (no indentation), it should have the highest level of scope and be usable throughout the whole script (after it's defined, of course). If the variable definition is indented and within a statement of some sort, such as a function definition, then it should only have the scope of that statement/function and be unusable outside of that statement/function.
I hope that helps clear things up for you!
Ok, for some reason YT won't let me reply to your reply, so new comment it is, with apology. I think what bugged me, and was poorly articulated is that I'm used to Python, and variables in Python script body does not fall under the scope accessible to functions unless you declare them as global, but I see lua handles things differently.
A little. Lua has a global table that stores any global variables able to be used across multiple scripts, with some exceptions, but declaring a variable as a local variable using the `local' keyword sets the scope of the variable to the block it's declared in. If it's declared on it's own, the "block" of code it's scope is limited to is the script itself. If declared in a function or other block within that script, then its scope is obviously limited to that specific block instead.
It's a little trickier in Roblox than just a Lua script on general though.