Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to use Global Variables?

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.

6 comments

0

Can you include your two scripts? It would make explaining it easier

0

I only have 1 because I dont understand the global variables.

0

_G, also you can use remote events..

0

_G.variablename

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.

0

Ok i did it.

2 answers

✓ Accepted answer

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.