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: | ||
== 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> | ||
== | === Overwritting === | ||
A variable can be changed simply by assigning another value to it: | |||
<pre> | <pre> | ||
n = 12 | |||
print(n) --> 12 | |||
n = 24 | |||
print(n) --> 24 | |||
print( | |||
</pre> | </pre> | ||
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. <!-- call me on this --> | |||
<pre> | <pre> | ||
LETTERS = "ABC" -- valid | |||
x86 = 1978 -- valid | |||
var_name = "variable" -- valid | |||
_ = "blank" -- valid | |||
if = 12 -- NOT valid! | |||
16th = "Lincoln" -- NOT valid! | |||
</pre> | </pre> | ||
<!-- | |||
== Variable Scope == | |||
local, global, shared | |||
=== 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!