I do not know how to use global variables.
I am using a while loop and using a variable to determine whether or not it is true.
I want to change this variable through another script, although I need to use a global variable for that I believe. But I cannot use a global variable because I do not know how to use them.
Can I please get some help with this.
1 | _G.Apple = true |
1 | while Apple = = true do |
It says that Apple still is undefined.
Your mistake here, is that you are trying to reference Apple without defining it, meaning that you have to use _G.Apple
Instead of
1 | while Apple = = true do |
Try it like this.
1 | while _G.Apple = = true do |
Hello.
Globals:
There are two kinds of global variables.
These variables should be avoided unless you have a specific reason to use them.
Example:
1 | local function myFunc() |
2 | message = "Hello world!" |
3 | end |
4 |
5 | myFunc() |
6 |
7 | print (message) |
If we had a "local" before declaring the variable on line 2, it would print nil as of local variable scope.
_G
.These variables are variables that can be shared across all scripts, except client to server.
Here is your fixed script.
1 | _G.Apple = true |
2 |
3 | while _G.Apple = = true do |
4 |
5 | end |
You just forgot to write _G
before writing the "Apple" variable.