<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.figuramc.org/index.php?action=history&amp;feed=atom&amp;title=User%3ASlyme%2FDrafts%2FLua_Basics%2FVariables</id>
	<title>User:Slyme/Drafts/Lua Basics/Variables - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.figuramc.org/index.php?action=history&amp;feed=atom&amp;title=User%3ASlyme%2FDrafts%2FLua_Basics%2FVariables"/>
	<link rel="alternate" type="text/html" href="https://wiki.figuramc.org/index.php?title=User:Slyme/Drafts/Lua_Basics/Variables&amp;action=history"/>
	<updated>2026-05-29T17:17:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.figuramc.org/index.php?title=User:Slyme/Drafts/Lua_Basics/Variables&amp;diff=562&amp;oldid=prev</id>
		<title>Slyme: First- nope, Second Draft</title>
		<link rel="alternate" type="text/html" href="https://wiki.figuramc.org/index.php?title=User:Slyme/Drafts/Lua_Basics/Variables&amp;diff=562&amp;oldid=prev"/>
		<updated>2024-10-07T03:28:06Z</updated>

		<summary type="html">&lt;p&gt;First- nope, Second Draft&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Notice&lt;br /&gt;
|content=&amp;#039;&amp;#039;&amp;#039;This article is part of a larger series on the [[../|Lua Basics]].&amp;#039;&amp;#039;&amp;#039;&amp;lt;br&amp;gt;&lt;br /&gt;
* Please check out the [[Lua Basics]] if you haven&amp;#039;t already.&lt;br /&gt;
* For more documents within this series, please scroll to the bottom of the page or [[#See more|click here]].&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Variables&amp;#039;&amp;#039;&amp;#039; are the way Lua stores values for later use. Think of them like several labeled boxes. For our purposes, each box contains one [[../Values|value]]. Whenever you ask for the contents of a specific variable, Lua looks for it, finds it, and reports back with the contents.&lt;br /&gt;
==Declaring a Variable==&lt;br /&gt;
Variables can be made by typing a variable name, an equal sign (&amp;lt;code&amp;gt;=&amp;lt;/code&amp;gt;), and a value or expression. They can then be used in place of values and can be modified later within a script.&lt;br /&gt;
{{Script&lt;br /&gt;
|script=a = &amp;quot;Hello, World!&amp;quot; -- Here, we store the string value &amp;quot;Hello, World!&amp;quot; within the variable &amp;quot;a&amp;quot;&lt;br /&gt;
print(a) --&amp;gt; Hello, World!&lt;br /&gt;
a = a .. &amp;quot; You are amazing!&amp;quot; -- We add &amp;quot; You are amazing!&amp;quot; to the end of &amp;quot;Hello, World!&amp;quot; to form...&lt;br /&gt;
print(a) --&amp;gt; Hello, World! You are amazing!&lt;br /&gt;
}}&lt;br /&gt;
Lua is a dynamically typed language, which means that any variable can hold any value regardless of what it originally held.&lt;br /&gt;
{{Script&lt;br /&gt;
|script=a = &amp;quot;Hello, World!&amp;quot;&lt;br /&gt;
a = 5&lt;br /&gt;
a = true&lt;br /&gt;
print(a) --&amp;gt; true&lt;br /&gt;
}}&lt;br /&gt;
===Valid Names===&lt;br /&gt;
Variable names...&lt;br /&gt;
* Can only use alphanumeric characters and underscores (&amp;lt;code&amp;gt;_&amp;lt;/code&amp;gt;).&lt;br /&gt;
* Must not start with a number.&lt;br /&gt;
* Cannot be the same as a Lua keyword (&amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt;, etc.)&lt;br /&gt;
For instance...&lt;br /&gt;
* ✅ &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; is a valid variable name.&lt;br /&gt;
* ✅ &amp;lt;code&amp;gt;_b&amp;lt;/code&amp;gt; is a valid variable name.&lt;br /&gt;
* ✅ &amp;lt;code&amp;gt;aBc123&amp;lt;/code&amp;gt; is a valid variable name.&lt;br /&gt;
* ❌ &amp;lt;code&amp;gt;123abc&amp;lt;/code&amp;gt; is an &amp;#039;&amp;#039;&amp;#039;invalid&amp;#039;&amp;#039;&amp;#039; variable name; variable names cannot start with a number.&lt;br /&gt;
* ❌ &amp;lt;code&amp;gt;foo-bar&amp;lt;/code&amp;gt; is an &amp;#039;&amp;#039;&amp;#039;invalid&amp;#039;&amp;#039;&amp;#039; variable name; variable name can only use alphanumeric characters and underscores (&amp;lt;code&amp;gt;_&amp;lt;/code&amp;gt;)&lt;br /&gt;
* ❌ &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt; is an &amp;#039;&amp;#039;&amp;#039;invalid&amp;#039;&amp;#039;&amp;#039; variable name; variable names cannot be the same as a Lua keyword.&lt;br /&gt;
* ❌ &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; is an &amp;#039;&amp;#039;&amp;#039;invalid&amp;#039;&amp;#039;&amp;#039; variable name; variable names cannot be the same as a Lua keyword.&lt;br /&gt;
==Issues with Global Variables==&lt;br /&gt;
By default, new variables declared within Lua are global. This means that they can be used &amp;#039;&amp;#039;anywhere&amp;#039;&amp;#039; within the Lua environment once declared. This can lead to some adverse effects.&lt;br /&gt;
&lt;br /&gt;
For starters, scripts can cause interference with other scripts if they all use the same name for a global variable.&lt;br /&gt;
&lt;br /&gt;
These issues can be fixed with [[#Local Variables|local variables]].&lt;br /&gt;
==Local Variables==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Local variables&amp;#039;&amp;#039;&amp;#039; are limited to their own [[#Variable Scope|scope]], meaning that only instructions within it can access their contents. A new local variable can be created by putting &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; behind the first declaration of the variable.&lt;br /&gt;
&lt;br /&gt;
To use the same local variable within the same scope, make sure to &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; use the &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; tag for the variable again. If not...&lt;br /&gt;
{{Script&lt;br /&gt;
|script=local var = 5&lt;br /&gt;
do&lt;br /&gt;
    print(var) --&amp;gt; 5&lt;br /&gt;
    local var = var*2&lt;br /&gt;
    print(var) --&amp;gt; 10&lt;br /&gt;
end&lt;br /&gt;
print(var) --&amp;gt; 5&lt;br /&gt;
}}&lt;br /&gt;
&amp;lt;includeonly&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
==Global Variables==&lt;br /&gt;
Global variables are very simple: from the point of its declaration onward, any script in the environment who doesn&amp;#039;t have a local variable with the same name can use the global variable. This can lead to adverse effects.&lt;br /&gt;
&lt;br /&gt;
For one, globals may or may not be defined. If not, trying to use it will result in a {{Type|nil}} being returned instead of a useful value. This may not be what you want in most cases, especially when you intend to use it in an expression, in which case your script will most likely error.&lt;br /&gt;
{{Script&lt;br /&gt;
|name=script-1.lua&lt;br /&gt;
|script=-- ...&lt;br /&gt;
print(c*2) -- ERROR! attempt to perform arithmetic on a nil value (global &amp;#039;c&amp;#039;)&lt;br /&gt;
-- ...&lt;br /&gt;
}}{{Script&lt;br /&gt;
|name=script-2.lua&lt;br /&gt;
|script=-- ...&lt;br /&gt;
c = 10&lt;br /&gt;
-- ...&lt;br /&gt;
}}&lt;br /&gt;
==Local Variables==&lt;br /&gt;
Whenever a local variable is declared, its scope will extend to the end of the block it is currently in. For example...&lt;br /&gt;
{{Script&lt;br /&gt;
|script=local var = 5&lt;br /&gt;
do&lt;br /&gt;
    print(var) --&amp;gt; 5&lt;br /&gt;
    local var = var*2&lt;br /&gt;
    print(var) --&amp;gt; 10&lt;br /&gt;
end&lt;br /&gt;
print(var) --&amp;gt; 5&lt;br /&gt;
|highlight=3-5,7&lt;br /&gt;
}}&lt;br /&gt;
* The first print instruction on line three will print the local variable just outside the &amp;lt;code&amp;gt;do ... end&amp;lt;/code&amp;gt; block since that&amp;#039;s the last time a local variable within its scope was declared with that name.&lt;br /&gt;
* The local variable declaration on line four is able to use the old &amp;lt;code&amp;gt;var&amp;lt;/code&amp;gt; local variable since the one on line five only overshadows it &amp;#039;&amp;#039;after&amp;#039;&amp;#039; its declaration.&lt;br /&gt;
* The second print instruction on line five will use the newly declared local variable &amp;lt;code&amp;gt;var&amp;lt;/code&amp;gt; since it&amp;#039;s the closest within its scope.&lt;br /&gt;
* The final print instruction on line seven will use the older local variable &amp;lt;code&amp;gt;var&amp;lt;/code&amp;gt; since the newer one on line four is now out of scope since it was defined in a block further inside.&lt;/div&gt;</summary>
		<author><name>Slyme</name></author>
	</entry>
</feed>