Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to use Global Variables?

Asked by 4 years ago
Edited 4 years ago

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.

_G.Apple = true
while Apple == true do

It says that Apple still is undefined.

0
Can you include your two scripts? It would make explaining it easier SteamG00B 1633 — 4y
0
I only have 1 because I dont understand the global variables. saulty11 17 — 4y
0
_G, also you can use remote events.. greatneil80 2647 — 4y
0
_G.variablename Rinpix 639 — 4y
View all comments (2 more)
0
_G = Apple. That did not work. Also, I need to know how to put this into another script properly as well as update the value. saulty11 17 — 4y
0
Ok i did it. saulty11 17 — 4y

2 answers

Log in to vote
0
Answered by
xXLuka_XD 103
4 years ago

Your mistake here, is that you are trying to reference Apple without defining it, meaning that you have to use _G.Apple

Instead of

while Apple == true do

Try it like this.

while _G.Apple == true do
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hello.

Globals:

There are two kinds of global variables.

  • Variables without "local" before them.

These variables should be avoided unless you have a specific reason to use them.

Example:

local function myFunc()
    message = "Hello world!"
end

myFunc()

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.

_G.Apple = true

while _G.Apple == true do

end

You just forgot to write _G before writing the "Apple" variable.

Answer this question