Variables: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Anaminus
making this about variables
>Anaminus
No edit summary
Line 1: Line 1:
__TOC__
== Introduction ==
== Introduction ==


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


__TOC__
== Assignment ==
== Assignment ==


Line 21: Line 20:
</pre>
</pre>


== Names ==
=== Overwritting ===


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.
A variable can be changed simply by assigning another value to it:


<pre>
<pre>
LETTERS = "ABC"          -- valid
n = 12
x86 = 1978              -- valid
print(n)   --> 12
var_name = "variable"   -- valid
_ = "blank"              -- valid
16th = "Lincoln"        -- NOT valid!
</pre>
 
== 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:


<pre>
n = 24
x = 2 -- This assigns x to the value of 2
print(n)   --> 24
y = 3 -- This assigns y to the value of 3
print(x + y) -- This prints out the sum of (x+y))
</pre>
</pre>


* Test the script by pressing the play button [[Image:Play.JPG]]
Even though ''n'' was assigned to a value of 12, it was overwritten by the second assignment of 24.
* Your Output bar should show the number 5, which is the sum of x+y.


== In depth ==
== Names ==


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":
The name you give a variable can only contain letters (uppercase and lowercase), numbers, or an underscore ( _ ). A variable name cannot be a reserved [[keyword]] or start with a number. Other than that, there are no restrictions. <!-- call me on this -->


<pre>
<pre>
sometext = "Hello"
LETTERS = "ABC"         -- valid
luckynumber = 93
x86 = 1978              -- valid
brick5 = "Green"
var_name = "variable"   -- valid
VariableIDoNotWantToDefineJustYet = nil
_ = "blank"              -- valid
</pre>


To change a variable, you just do the same thing:
if = 12                  -- NOT valid!
 
16th = "Lincoln"         -- NOT valid!
<pre>
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...
</pre>
</pre>


Please note that a variable cannot start with a number, nor can it be a keyword. Which means these variable names are invalid:
<!--
== Variable Scope ==


<pre>
local, global, shared
-- DON'T EVER USE THESE NAMES WHEN ASSIGNING VARIABLES --
if = 1
12 = 2
do = "hello"
</pre>


While these and many others aswell are valid:
<pre>
a = 1
hello = "hey"
mom = "nice"
</pre>


=== Local Variables ===
=== Local Variables ===
Line 138: Line 99:
[[Intro to Scripting: Make a Dance Floor]]
[[Intro to Scripting: Make a Dance Floor]]
[[Absolute beginner's guide to scripting]]
[[Absolute beginner's guide to scripting]]
-->
[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Revision as of 10:59, 28 September 2010

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

Overwritting

A variable can be changed simply by assigning another value to it:

n = 12
print(n)    --> 12

n = 24
print(n)    --> 24

Even though n was assigned to a value of 12, it was overwritten by the second assignment of 24.

Names

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

LETTERS = "ABC"          -- valid
x86 = 1978               -- valid
var_name = "variable"    -- valid
_ = "blank"              -- valid

if = 12                  -- NOT valid!
16th = "Lincoln"         -- NOT valid!