Variables

From Legacy Roblox Wiki
Revision as of 10:37, 28 September 2010 by >Anaminus (making this about variables)
Jump to navigationJump to search

Introduction

A variable is a name you can use to hold a value. In Lua, a variable can have any data type, such as a number or a string.

Assignment

Assigning a value to a variable can be done using the = operator, like so:

x = 10
word = "Hello"

Variable x now has a value of the number 10, and word has a value of the string "Hello". If we print out these variables, we should see the value they have:

print(x)     --> 10
print(word)  --> Hello

Names

The name you give a variable can only have letters (uppercase and lowercase), numbers, or an underscore ( _ ). A variable name cannot start with a number. Other than that, there are no restrictions.

LETTERS = "ABC"          -- valid
x86 = 1978               -- valid
var_name = "variable"    -- valid
_ = "blank"              -- valid
16th = "Lincoln"         -- NOT valid!

Scripting

Open Roblox Studio and make sure that there aren't any other scripts in the Explorer window. We're going to start off with a fresh script.

  • Click Workspace in the Explorer Window.
  • Click Insert > Object > Script.
  • Double click the newly inserted script.
  • A blank window should open up.
  • Insert the following script:
x = 2 -- This assigns x to the value of 2
y = 3 -- This assigns y to the value of 3
print(x + y) -- This prints out the sum of (x+y))
  • Test the script by pressing the play button File:Play.JPG
  • Your Output bar should show the number 5, which is the sum of x+y.

In depth

Variables are essential to use in a good script. A variable is a shortcut to values that you can store, then retrieve and/or change later. Defining a variable is very easy, as well. To define a variable, you just type "variableName = value":

sometext = "Hello"
luckynumber = 93
brick5 = "Green"
VariableIDoNotWantToDefineJustYet = nil

To change a variable, you just do the same thing:

variable = 1 -- This assigns "variable" to the value 1.
print(variable) -- This prints the variable, which is 1 now.

variable = 7 -- This makes the variable 7 instead of 1.
print(variable) -- This prints 7, because "variable" is now equal to 7.

variable = variable + 5 -- Adds 5 to the variable, making the variable 7+5.
print(variable) -- This prints 12, because 7 + 5 is 12, you should know this math...

Please note that a variable cannot start with a number, nor can it be a keyword. Which means these variable names are invalid:

-- DON'T EVER USE THESE NAMES WHEN ASSIGNING VARIABLES --
if = 1
12 = 2
do = "hello"

While these and many others aswell are valid:

a = 1
hello = "hey"
mom = "nice"

Local Variables

A local variable is a variable that is ONLY known to it's scope of the script. To define one you have to have "local" before it:

local var = "Hello" -- This assigns var to the string "Hello"

You might not understand what these scopes are, just yet, so let me show you:

function Test() -- Creates a new scope (Test)
local A = 1
print(A)
end -- This ends the scope 'Test', making the variable A unaccessable outside.

Test() -- Runs the function above
print(A) -- Tries to print A, but since A is local to the 'Test', it prints nothing

Which will print 1 and then nil, because the variable A can only be accessed from within the 'Test'.

Global Variables

A global variable is a variable stored in the environment of your script and is accessible to the entire script. This is the default when the "local" keyword is not used.

Shared Variables

A shared variable is a variable that is "readable" by all scripts. For example, one is 'Game'. When you state 'Game' it thinks of the Data Model that is your game. To define a such shared variable yourself, you used the table "shared", ".", and then the variable name. Then when another script states the variable, it will think of the variable you set it to:

Script 1

shared.Var = 10 -- This defines the variable 'Var' to a shared variable, and set it equal to 10.

Script 2

wait(1) -- If Script 2 runs before Script 1, the shared variable will not have been defined...
print(shared.Var) -- This prints 10 in the output! =D

See Also

Intro to Scripting: Make a Dance Floor Absolute beginner's guide to scripting