Variables

From Legacy Roblox Wiki
Revision as of 19:43, 27 September 2010 by >Oysi93 (If you mind about the 'johndoe' -> 'oysi93', then change back. I will make this article better tomorow, I promise! =) (A lot here was wrong))
Jump to navigationJump to search

Introduction

This guide is for absolute beginners. It is intended to familiarize you with Variables in Lua. If you haven't already, please see Your first script as a beginner tutorial.

What are we trying to do?

At the end of this guide, you will be able to:

  • Know what a variable is
  • Create a variable
  • Assign a value to a variable
  • Use a variable in a mathematical equation

Setup

You will need to open Roblox Studio. Once you have done that, you will need to click "My ROBLOX", select your map, and click "Edit". At this point, you should see your familiar map.

You will need some essentials in Roblox Studio to make this work:

  • The Output window:

File:Outputwindow.JPG

  • The Explorer window:

and

  • The Command bar:

  • To make sure you have the Output window, click View / Output.
  • To make sure you have the Explorer window visible, click View / Explorer.
  • To make sure you have the Command bar visible, click View / Toolbars / Command.

Discussion

A variable is a word or letter you can assign a value to. For instance, in algebra you can give "x" the value of 10. In lua, there's a little more liberty -- Not only can a variable be a number, but it can also be a word, like your name:

x = 1
myname = "oysi93"

Notice that the word "oysi93" is in quotes, but the number 1 is not in quotes. That's because "oysi93" is considered a String, or in other words, considered as text. In short, you can perform arithmetic on numbers, but not on "oysi93".

Arithmetic can be performed on variables, just as on numbers. For example, if x is equal to 2 and y is equal to 3, then x+y would be 5. Let's try that in the scripting section:

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 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 global variable yourself, you state "_G." and then the variable name, table indexing if you know what that is. Then when another script states the variable, it will think of the variable you set it to:

Script 1

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

Script 2

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

See Also

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