In-Depth Scripting Guide: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Tomtomn00
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
Tags: mobile web edit mobile edit
 
(31 intermediate revisions by 10 users not shown)
Line 2: Line 2:


==What You Need!==
==What You Need!==
*[[Roblox_Studio|'''ROBLOX Studio''']]
*[[Studio|'''ROBLOX Studio''']]
*'''Explorer Tab''' - This tells you exactly where everything is.
*[[Explorer_Window|'''Explorer Tab''']] - This tells you exactly where everything is.
*'''Properties Tab''' - This tells you all the things editable in an object.
*[[Properties_Window|'''Properties Tab''']] - This tells you all the things editable in an object.
*'''Output''' - A scripter's best friend. It tells you any mistakes (syntax, values, etc) found in a script, and where to find it.
*[[Output|'''Output''']] - A scripter's best friend. It tells you any mistakes (syntax, values, etc) found in a script, and where to find it.
*'''Command Prompt (Optional)''' - Not really needed, but is a great way to test out one-lined wonders.
*[[Command_Bar|'''Command Bar (Optional)''']] - Not really needed, but is a great way to test out one-lined wonders.
*'''Any script; new or old (Optional)''' - If you want an example script to look at, you can always pick one out of the toolbox.
*'''Any script; new or old (Optional)''' - If you want an example script to look at, you can always pick one out of the toolbox.


Line 17: Line 17:


Example:
Example:
<pre>
<syntaxhighlight lang="lua">
example = game.Workspace.Example
example = Workspace.Example
</pre>
</syntaxhighlight>


Notice how the variable has a name, which is "example."  The name can be anything except numbers alone.  If you have a name of just numbers and you use it in a script, you will end up with a syntax error.  When defining a variable, you need to tell exactly where it is.  The Explorer tab helps with this as there is always a little tab telling you that that object is a child of something.  When typing the parental of the object you want, spell the parental(s) correctly.  You do not need the function ":FindFirstChild()" when making variables.
Notice how the variable has a name, which is "example."  The name can be anything except numbers alone.  If you have a name of just numbers and you use it in a script, you will end up with a syntax error.  When defining a variable, you need to tell exactly where it is.  The Explorer tab helps with this as there is always a little tab telling you that that object is a child of something.  When typing the parental of the object you want, spell the parental(s) correctly.  You do not need the function ":FindFirstChild()" when making variables.
Line 27: Line 27:


Example:
Example:
<pre>
<syntaxhighlight lang="lua">
bin = script.Parent
bin = script.Parent


function anchor(object)
function anchor(object)
object.Anchored = true
object.Anchored = true
end
end


function onTouched(part)
function onTouched(part)
anchor(part)
anchor(part)
end
end


bin.Touched:connect(onTouched)
bin.Touched:connect(onTouched)
</pre>
</syntaxhighlight>
There is the one variable, not really needed but I need to stress its importance.  In the first function, as in every function, the term "function" is first.  It is correct if the term turns purple.  After the term "function," the function needs to be named (in the example, there are two -- named "anchor" and "onTouched".)  Do not use numbers as a name.  It is better to spell out numbers in names, except in some occasions.  Now, after the name, you'll see the word "object" in parentheses.  You can rename it anything you want, except numbers, and you can use it for anything you want.  It's like a function variable, you can use it to do other things and use it repeatedly with different terms. You can also add more variables to this by using commas. So, this example could have been made: "function anchor(object, statement)".  In this function, though, it is anchoring the object no matter what.  Finally, the "end" statement.   
There is the one variable, not really needed but I need to stress its importance.  In the first function, as in every function, the term "function" is first.  It is correct if the term turns purple.  After the term "function," the function needs to be named (in the example, there are two -- named "anchor" and "onTouched".)  Do not use numbers as a name.  It is better to spell out numbers in names, except in some occasions.  Now, after the name, you'll see the word "object" in parentheses.  You can rename it anything you want, except numbers, and you can use it for anything you want.  It's like a function variable, you can use it to do other things and use it repeatedly with different terms. You can also add more variables to this by using commas. So, this example could have been made: "function anchor(object, statement)".  In this function, though, it is anchoring the object no matter what.  Finally, the "end" statement.   


Line 45: Line 45:


===Connection Lines===
===Connection Lines===
it If you look in the above example, you will notice a connection line. A connection line does what it says, connects the function to something done.  The layout for a connection line is very simple. This is also one of the parts of a script when the scripter is most happy, before they test it out, that is.  The connection lines usually tell you that the script is done and it is time to test.
If you look in the above example, you will notice a connection line. A connection line does what it says, connects the function to something happening in a place.  The layout for a connection line is very simple. Connection lines usually tell you that the script is doing and when.


Example:
Example:
<pre>
<syntaxhighlight lang="lua">
bin = script.Parent
bin = script.Parent


function anchor(object)
function anchor(object)
object.Anchored = true
object.Anchored = true
end
end


function onTouched(part)
function onTouched(part)
anchor(part)
anchor(part)
end
end


bin.Touched:connect(onTouched)
bin.Touched:connect(onTouched) --this is called the "connection line"
</pre>
</syntaxhighlight>
Presuming this script is inside of a brick, it can therefore be touched.  In the variables, the brick is called "bin."  In the connection line, there is a specific order in which the line should be.  It should be like this:
Presuming this script is inside of a brick, it can therefore be touched.  In the variables, the brick is called "bin."  In the connection line, there is a specific order in which the line should be.  It should be like this:
<pre>(Object).(Action):connect((Function Name))</pre>
<pre>&lt;Object&gt;.&lt;Event&gt;:connect(&lt;Function&gt;)</pre>
The object "bin," is therefore being "Touched" and we are connecting it being touched with the function "onTouched."
The object "bin" (a brick) has an event called "Touched" which is being connected to the function "onTouched".


NOTE: To find actions, check in the Object Browser.  To get to the Object Browser, go into ROBLOX Studio, press "Help" on top, then hit Object Browser.  When there, you can find what you can do to objects by clicking on their name and scrolling all the way down. The actions are the ones with a lightning bolt next to them.
To find events for objects, check the [[Object_Browser|Object Browser]].


==Basic Lua Terms==
==Basic Lua Keywords==
Throughout Lua, you will find terms like 'if', 'local', and 'for'. In this section, I will teach you Basic Lua terms.  I recommend, if you haven't, to read the <b>Order of Operations</b> section of this guide before going on with this one.
Throughout Lua, you will find terms like 'if', 'local', and 'for'. In this section, I will teach you Basic Lua terms.  I recommend, if you haven't, to read the <b>Order of Operations</b> section of this guide before going on with this one.


Line 73: Line 73:
The if control structure takes one value directly after "if", and then the keyword "then", followed by a block of code that finishes with "end" (or an else statement). The if statement ''always'' executes the block of code after "then" ''unless'' the value given to it is either '''nil''' or '''false'''.
The if control structure takes one value directly after "if", and then the keyword "then", followed by a block of code that finishes with "end" (or an else statement). The if statement ''always'' executes the block of code after "then" ''unless'' the value given to it is either '''nil''' or '''false'''.


[http://www.lua.org/pil/4.3.1.html]
[http://www.lua.org/pil/4.3.1.html Lua 'If' Statement]


====Example One====
====Example One====
<pre>
<syntaxhighlight lang="lua">
brick = game.Workspace:findFirstChild("Brick")
brick = Workspace:findFirstChild("Brick")
if brick then
if brick then
print("The brick is here!")
print("The brick is here!")
end
end
</pre>
</syntaxhighlight>
In this snippet of code, the brick variable is defined as whatever is returned from findFirstChild. The if statement will check if the `brick` variable is neither nil nor false. If the findFirstChild function returned a brick, then the if statement block of code will be run.
In this snippet of code, the brick variable is defined as whatever is given by FindFirstChild. The if statement will check if the `brick` variable is neither nil nor false. If the FindFirstChild function returned a brick, then the if statement block of code will be run.


'''A common mistake when writing if-blocks is using a single equals sign instead of two. One equals sign is an assignment operator, not an equality operator. Use two equals signs when checking for equality!'''
'''A common mistake when writing if-blocks is using a single equals sign instead of two. One equals sign is an assignment operator, not an equality operator. Use two equals signs when checking for equality!'''


====Example Two====
====Example Two====
<pre>
<syntaxhighlight lang="lua">
number = 0
number = 0
while number < 10 do
while number < 10 do
wait(1)
wait(1)
number = number +1
number = number +1
if number > 5 then
if number > 5 then
print("Number Greater Than 5!")
print("Number Greater Than 5!")
end
end
end
end
</syntaxhighlight>
</pre>
On the first line, "number" is 0.  The next step is a 'while true do' loop.  It waits one second then "number" will increase by one.  Then there is the 'if' statement.  It is now asking if "number" is greater than 5.  If "number" is greater than 5, it will print, "Number Greater Than 5!"  If it isn't greater than 5, then it will just skip it and repeat the loop.  Once "number" turns to 11, the loop will end.  This also goes with connection functions.
On the first line, "number" is 0.  The next step is a 'while true do' loop.  It waits one second then "number" will increase by one.  Then there is the 'if' statement.  It is now asking if "number" is greater than 5.  If "number" is greater than 5, it will print, "Number Greater Than 5!"  If it isn't greater than 5, then it will just skip it and repeat the loop.  Once "number" turns to 11, the loop will end.  This also goes with connection functions.


Line 102: Line 102:


===The 'else' Statement===
===The 'else' Statement===
Say you have an 'if' function, but it doesn't meet the requirements. That's when the 'else' kicks in. It does the function of the else onwards.   
Say you have an 'if' function, but it has false or nil inside. That's when the 'else' kicks in. It does the function of the else onward to the end statement.   
 
<syntaxhighlight lang="lua">
 
<pre>
Example:
Example:
if game.Workspace == game.Lighting then
if Workspace == game.Lighting then
print("NOWAI!!!")
print("No way!")
else
else
print("Whew. That was close.")
print("Whew. That was close.")
end
end
</pre>
</syntaxhighlight>
Well, there's the 'if' statement, which is impossible. Now, if it can't fulfill the requirement, it goes to the 'else' statement. So it does its function and prints to the output the statement in quotes. Then the end.  
Well, there's the 'if' statement's condition, which returns false. It then goes to the 'else' statement and executes to the end statement.


===The 'elseif' Statement===
===The 'elseif' Statement===
'Elseif' is not an 'if' function. So what is the difference between 'elseif' and 'else if'?  'Elseif' is counted as an 'else' with a requirement for it to be run, and doesn't need an end. On the other hand, 'else if' is comprised of two statements: an else, and an if.  Therefore, it needs a 'end' for the 'if' statement. 'Elseif' is ALOT more secure that 'else', because the lines of script after it '''only''' runs if the condition after the 'elseif' is met. Unless you want the folowing lines to be run '''only ''' if the 'if' statement is false, then use 'elseif' to make sure that the lines only run if the 'elseif' condition is met.
'Elseif' is not an 'if' function. So what is the difference between 'elseif' and 'else if'?  'Elseif' is counted as an 'else' with a requirement for it to be run, and doesn't need an end. On the other hand, 'else if' is comprised of two statements: an else, and an if.  Therefore, it needs a 'end' for the extra 'if' statement. 'Elseif' is A secure than 'else', because the lines of script after it '''only''' runs if the condition after the 'elseif' is met. Unless you want the folowing lines to be run '''only ''' if the 'if' statement is false, then use 'elseif' to make sure that the lines only run if the 'elseif' condition is met.


<pre>
<syntaxhighlight lang="lua">
waffleboy = true
--GOOD
score = 12


Example 1:
if score < 5 then --if score is below 5
if waffleboy == false then
print("Losing!")
print("NOWAI!!")
elseif score > 10 then --if score is above 10
elseif waffleboy == true then
print("Winning!")
print("YAWAI!!")
else --logically, if score is between 5 and 10 (inclusively)
print("Doing OK!")
end
end
</syntaxhighlight>


Example 2A:
<syntaxhighlight lang="lua">
if waffleboy == false then
--BAD
print("NOWAI!!")
else if waffleboy == true then
print("YAWAI!!")
end
end


Example 2B:
if score < 5 then
if waffleboy == false then
print("Losing!")
print("NOWAI!!")
else
else
if waffleboy == true then
if score > 10 then
print("YAWAI!!")
print("Winning!")
end
end
end
end
</syntaxhighlight>
</pre>


'Elseif' doesn't need an end, but 'else if' does, because Example 2A and Example 2B are the same.
'Elseif' doesn't need an end, but 'else if' does, because Example 2A and Example 2B are the same.


===The 'repeat' Statement===
===The 'repeat' Statement===
This will keep on repeating what is put after until the 'until' statement is achieved.
This will execute the block of code once, then keep on repeating until the 'until' condition is neither nil nor false.


<pre>
<syntaxhighlight lang="lua">
a = 0
a = 0
repeat
repeat
a = a + 0.1
a = a + 0.1
until (a = 1) then
until a == 1
print("a equals to 1!")
print("a equals to 1!")
end
</syntaxhighlight>
</pre>


===The 'until' Statement===
===The 'end' Statement===
The until statement always goes after a repeat statement. Whatever is inbetween 'repeat' and 'until' will be repeated until the statement in between the parentheses is achieved.
This statement terminates an "if", "while", "for" or "function" statement's chunk of code.
<pre>
a = 0


repeat
a = a + 1
until a == 8
print("a equals 8")
</pre>
===The 'end' Statement===
Always remember to put this to end an 'if' statement.  This can also end the 'while' functions and 'for' functions and functions themselves. Also, either 'repeat' or 'until' statement requires an end. EIther way, you will still need one.
Example for 'if':
Example for 'if':
<pre>
<syntaxhighlight lang="lua">
 
value = true
value = true


if value == true then
if value then
print ("You are correct!")
print ("You are correct!")
end
end
</pre>
</syntaxhighlight>


Example for 'for':  
Example for 'for':  
<pre>
<syntaxhighlight lang="lua">
 
children = Workspace:GetChildren()
a = game.Workspace:GetChildren()
for k, child in pairs(children) do
for i = 1, #a do
if child:IsA('Part') and not child.Locked then
if a[i].className == "Part" and a[i].Locked == false then
child:Destroy()
a[i]:Remove()
end
end
end
</pre>
</syntaxhighlight>


Example for 'while':
Example for 'while':
<pre>
<syntaxhighlight lang="lua">
a = 0
while Workspace:FindFirstChild("Ball") do  
while true do  
Workspace.Ball:Destroy()
if a == 1 then break end
wait(1)
a = a + 0.1
wait(1)
end
end
end
</syntaxhighlight>
</pre>


Example for function:
Example for function:
<pre>
<syntaxhighlight lang="lua">
function clean(part)
function anchor(part)
part:Remove()
part.Anchored = true
end
end


clean(game.Workspace.Example)
anchor(Workspace.Base)
</pre>
</syntaxhighlight>


===The 'local' Statement===
===The 'local' Statement===
Whenever you are scripting and you canot make something a variable, like making a variable out of a function variable, you should use the term [http://www.lua.org/pil/4.2.html 'local'].
The [http://www.lua.org/pil/4.2.html 'local'] statement makes a variable usable in only the chunk of code it is defined in.


Example:
Example:
<pre>
<syntaxhighlight lang="lua">
bin = script.Parent
bin = script.Parent


function onTouched(part)
function onTouched(part)
local h = part.Parent:FindFirstChild("Humanoid")
local h = part.Parent:FindFirstChild("Humanoid")
if h~=nil then
if h then
print("Hi!")
while h.Health > 10 do
end
h.Health = h.Health - 10
wait(1)
end
end
end
end


bin.Touched:connect(onTouched)
bin.Touched:connect(onTouched)
</pre>
</syntaxhighlight>
'local' is just what it is, local. It is not a global statement like variables are, so it can be used in a whole different function, as long as it isn't stated already in the same function.
 
Remember if you (for example) define a local variable for a global variable, and you edit the local variable, the global variable will not change, unless you put the local = global
For the above code, if the variable `h` was not set as local, and two people touched the brick, only one person would be taking damage. However, since the `h` variable is local, it re-defines a new local `h` variable each time the function is called, thus allowing the function to not bug up when running twice at the same time.
<pre>
 
local a = game.Workspace:findFirstChild("Score").Value
If you don't use the local keyword, any assignment is considered the opposite - global. Global things can be used anywhere in a script (not just in the chunk of code it is defined in). Consider this:
while true do
 
a = a + 1
<syntaxhighlight lang="lua">
game.Workspace:findFirstChild("Score").Value = a -- This line basically "Updates" the real varible value with the "a" value.
a = "apple" --global `a` is "apple"
wait(1)
 
function doSomething()
local a = "banana"
print(a) --> "banana"
a = nil --sets the local variable to nil
print(a) --> "apple" (using the global variable)
end
end
</pre>


===The 'while' Condition===
doSomething()
<b>While</b> is a condition.  If it is false, the loop will end.  If it is true, the loop will be executed, and the process will be repeated.[http://www.lua.org/pil/4.3.2.html]
</syntaxhighlight>
 
===The 'while' Statement===
The [http://www.lua.org/pil/4.3.2.html while] statement is another kind of loop. The block of code will be executed as long as the condition between 'while' and 'do' is neither nil nor false. ''When using {{true}} as the condition, you should always use wait() in your while statements, else ROBLOX will freeze and crash!''


Example one:
Example one:
<pre>
<syntaxhighlight lang="lua">
while true do
while true do --this repeats forever since "true" is always neither nil nor false
wait(3)
wait(3)
print("Hello World!")
print("Hello World!")
end
</syntaxhighlight>
 
We can also instead of putting the wait after while true do, inside it, since wait never returns nil or false!
 
For example:
 
<syntaxhighlight lang="lua">
while wait(3) do
print("I waited three seconds, and this message came up")
end
end
</pre>
</syntaxhighlight>


Example two:
Another example:
<pre>
<syntaxhighlight lang="lua">
example = true
energy = 0
while example == true do
while energy < 10 do
wait(3)
wait(3)
print("Hello World!")
print("Charging up...")
example = false
print(energy)
energy = energy + 1
end
end
</pre>
</syntaxhighlight>
Example one shows a function that will never end. It will wait 3 seconds, then post into the output 'Hello World!'.  In example two, it does the same thing except this time, 'example' must be true for it to run. This one will wait 3 seconds, print the same thing, then turn its example to false. If you tried it, you will notice that it only runs once. Remember, all function statements need an 'end'.  Note:  Whenever a "while true do" is running, you <b>always</b> need a "wait()" statement. If you do not have a "wait()" statement, your place will not function and it will just hang, and lock up your program.
 
Example one shows a function that will never end. Example two shows how a condition is tried each time the loop ends.


===The 'for' Statement===
===The 'for' Statement===
This goes through all of the objects noted and chooses different objects based on your 'if' statements.
The 'for' statement is another type of loop. There are two kinds - ''generic'' and ''numeric''. One works with tables and iterator functions, and the other with numbers.
 
Numeric for is generally considered easier than generic because of it's simplicity. The format for numeric for statements is:
 
<syntaxhighlight lang="lua">
for a = b, c, d do
--code
end
</syntaxhighlight>


Example One:
Lua first defines a local variable `a` as `b` (`b` must be a number). It will then execute the code, and add `d` to `a`. It repeats the process until `a` is greater than or equal to `c`. You can leave out `d` and Lua assumes 1 as d.
<pre>
 
for i=1, 6 do
Example one: (numeric for)
print("Hi")
<syntaxhighlight lang="lua">
for i = 1, 10 do
print(i)
end
end
</pre>
</syntaxhighlight>
This repeats the function 6 times, each time printing the word "Hi".
This executes the block of code 10 times, incrementing i by 1 each time until it is 10. Notice how the delta amount (the amount that is added)
is omitted and Lua assumes 1.
 
Generic for statements are a bit more difficult because they use iterator functions. Iterator functions give you the next value in some kind of list each time they are run. Lua runs the function until the iterator function does not give a value back. That's really complicated to know. In Lua, you can use the pairs() function right inside the for statement to generate an iterator function for that table.
 
This is a lot easier done than said. Consider this example.


Example Two:
Example Two:
<pre>
{{Code and output
|code=
a = {"Hello", true, 1337}
a.derp = "herp"
for key, value in pairs(a) do
print(key, value)
end
|output=
1 Hello
2 true
3 1337
derp herp
}}
 
This runs the chunk of code for every value in the `a` table.
 
Generic for-statements can be used to easily go through the children of an object:
 
Example Three:
<syntaxhighlight lang="lua">
bricks = 0
bricks = 0
local c = game.Workspace:GetChildren()
a = Workspace:GetChildren()
for i=1, #c do
for k, v in pairs(a) do
if c[i].className == "Part" then
if v:IsA("Part") then
bricks = bricks + 1
bricks = bricks + 1
end
end
end
end
print("Bricks: " .. bricks)
print(bricks)
</syntaxhighlight>
</pre>
The first line tells you that there are 0 bricks.  Now we are naming our parent, "c", a very common term when using 'for'.  Now the 'for' statement. "for i=1, #c do" says "go through the all of 'c'"  Now the 'if' statement.  When noting each brick in a 'for' 'if' statement, you need to have it in this format: "c[i]" (Based on this example)  The script goes through everything in the Workspace, and if the object is a Part, the brick count goes up by one, then prints into the output the number it ends up on.
 


== See also ==
== See also ==


<br>[[Built-in functions]]
*[http://www.lua.org/pil Programming in Lua]
<br>[http://www.lua.org/pil/4.3.1.html Programming in Lua]


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 05:56, 27 April 2023

What You Need!

  • ROBLOX Studio
  • Explorer Tab - This tells you exactly where everything is.
  • Properties Tab - This tells you all the things editable in an object.
  • Output - A scripter's best friend. It tells you any mistakes (syntax, values, etc) found in a script, and where to find it.
  • Command Bar (Optional) - Not really needed, but is a great way to test out one-lined wonders.
  • Any script; new or old (Optional) - If you want an example script to look at, you can always pick one out of the toolbox.

Order of Operations; Script Style (VFC)

There is an order of which you should add your operations, just like in math. A mnemonic device for this order is "VFC", for "Variables, Functions, and Connections". A good script usually has itself in a neat and tidy order so that almost anyone can understand what the script does just by looking at it.

Variables

Usually in a script, there is a specific order in which you put your operations. First off, one of the most important things to put in the very beginning are variables. Variables are used to note objects that will be used a lot. If you like to be organized, you like to put things in models. Sometimes, that model chain can get pretty lengthy.

Example:

example = Workspace.Example

Notice how the variable has a name, which is "example." The name can be anything except numbers alone. If you have a name of just numbers and you use it in a script, you will end up with a syntax error. When defining a variable, you need to tell exactly where it is. The Explorer tab helps with this as there is always a little tab telling you that that object is a child of something. When typing the parental of the object you want, spell the parental(s) correctly. You do not need the function ":FindFirstChild()" when making variables.

Functions

Secondly, in many scripts, you will come across a function. A function is something that can be used repeatedly. Some functions are also used to shorten script length, and some are just there because someone didn't want to delete it. When I create functions, I create the ones without a connection first, and then I create the ones with a connection line afterwards. If I do it like this, I can use the non-connections in the ones with connections. After all the things inside, you always close a function with an "end" statement.

Example:

bin = script.Parent

function anchor(object)
	object.Anchored = true
end

function onTouched(part)
	anchor(part)
end

bin.Touched:connect(onTouched)

There is the one variable, not really needed but I need to stress its importance. In the first function, as in every function, the term "function" is first. It is correct if the term turns purple. After the term "function," the function needs to be named (in the example, there are two -- named "anchor" and "onTouched".) Do not use numbers as a name. It is better to spell out numbers in names, except in some occasions. Now, after the name, you'll see the word "object" in parentheses. You can rename it anything you want, except numbers, and you can use it for anything you want. It's like a function variable, you can use it to do other things and use it repeatedly with different terms. You can also add more variables to this by using commas. So, this example could have been made: "function anchor(object, statement)". In this function, though, it is anchoring the object no matter what. Finally, the "end" statement.

In the second function, the function has a connection line. It's the same as the first function, the "function" statement, the function name, and the variable. The difference is, the previous function anchors the so-called "part." The variable is defined as "part," which is in this case, the object being touched. Finally, the "end" statement as always.

Connection Lines

If you look in the above example, you will notice a connection line. A connection line does what it says, connects the function to something happening in a place. The layout for a connection line is very simple. Connection lines usually tell you that the script is doing and when.

Example:

bin = script.Parent

function anchor(object)
	object.Anchored = true
end

function onTouched(part)
	anchor(part)
end

bin.Touched:connect(onTouched) --this is called the "connection line"

Presuming this script is inside of a brick, it can therefore be touched. In the variables, the brick is called "bin." In the connection line, there is a specific order in which the line should be. It should be like this:

<Object>.<Event>:connect(<Function>)

The object "bin" (a brick) has an event called "Touched" which is being connected to the function "onTouched".

To find events for objects, check the Object Browser.

Basic Lua Keywords

Throughout Lua, you will find terms like 'if', 'local', and 'for'. In this section, I will teach you Basic Lua terms. I recommend, if you haven't, to read the Order of Operations section of this guide before going on with this one.

The if Statement

The if control structure takes one value directly after "if", and then the keyword "then", followed by a block of code that finishes with "end" (or an else statement). The if statement always executes the block of code after "then" unless the value given to it is either nil or false.

Lua 'If' Statement

Example One

brick = Workspace:findFirstChild("Brick")
if brick then
	print("The brick is here!")
end

In this snippet of code, the brick variable is defined as whatever is given by FindFirstChild. The if statement will check if the `brick` variable is neither nil nor false. If the FindFirstChild function returned a brick, then the if statement block of code will be run.

A common mistake when writing if-blocks is using a single equals sign instead of two. One equals sign is an assignment operator, not an equality operator. Use two equals signs when checking for equality!

Example Two

number = 0
while number < 10 do
	wait(1)
	number = number +1
	if number > 5 then
		print("Number Greater Than 5!")
	end
end

On the first line, "number" is 0. The next step is a 'while true do' loop. It waits one second then "number" will increase by one. Then there is the 'if' statement. It is now asking if "number" is greater than 5. If "number" is greater than 5, it will print, "Number Greater Than 5!" If it isn't greater than 5, then it will just skip it and repeat the loop. Once "number" turns to 11, the loop will end. This also goes with connection functions.

TIP: When using 'if' statements, use the proper Operations of comparison.

The 'else' Statement

Say you have an 'if' function, but it has false or nil inside. That's when the 'else' kicks in. It does the function of the else onward to the end statement.

Example:
if Workspace == game.Lighting then
	print("No way!")
else
	print("Whew. That was close.")
end

Well, there's the 'if' statement's condition, which returns false. It then goes to the 'else' statement and executes to the end statement.

The 'elseif' Statement

'Elseif' is not an 'if' function. So what is the difference between 'elseif' and 'else if'? 'Elseif' is counted as an 'else' with a requirement for it to be run, and doesn't need an end. On the other hand, 'else if' is comprised of two statements: an else, and an if. Therefore, it needs a 'end' for the extra 'if' statement. 'Elseif' is A secure than 'else', because the lines of script after it only runs if the condition after the 'elseif' is met. Unless you want the folowing lines to be run only if the 'if' statement is false, then use 'elseif' to make sure that the lines only run if the 'elseif' condition is met.

--GOOD
score = 12

if score < 5 then --if score is below 5
	print("Losing!")
elseif score > 10 then --if score is above 10
	print("Winning!")
else --logically, if score is between 5 and 10 (inclusively)
	print("Doing OK!")
end
--BAD

if score < 5 then
	print("Losing!")
else
	if score > 10 then
		print("Winning!")
	end
end

'Elseif' doesn't need an end, but 'else if' does, because Example 2A and Example 2B are the same.

The 'repeat' Statement

This will execute the block of code once, then keep on repeating until the 'until' condition is neither nil nor false.

a = 0
repeat
	a = a + 0.1
until a == 1
print("a equals to 1!")

The 'end' Statement

This statement terminates an "if", "while", "for" or "function" statement's chunk of code.

Example for 'if':

value = true

if value then
	print ("You are correct!")
end

Example for 'for':

children = Workspace:GetChildren()
for k, child in pairs(children) do
	if child:IsA('Part') and not child.Locked then
		child:Destroy()
	end
end

Example for 'while':

while Workspace:FindFirstChild("Ball") do 
	Workspace.Ball:Destroy()
	wait(1)
end

Example for function:

function anchor(part)
	part.Anchored = true
end

anchor(Workspace.Base)

The 'local' Statement

The 'local' statement makes a variable usable in only the chunk of code it is defined in.

Example:

bin = script.Parent

function onTouched(part)
	local h = part.Parent:FindFirstChild("Humanoid")
	if h then
		while h.Health > 10 do
			h.Health = h.Health - 10
			wait(1)
		end
	end
end

bin.Touched:connect(onTouched)

For the above code, if the variable `h` was not set as local, and two people touched the brick, only one person would be taking damage. However, since the `h` variable is local, it re-defines a new local `h` variable each time the function is called, thus allowing the function to not bug up when running twice at the same time.

If you don't use the local keyword, any assignment is considered the opposite - global. Global things can be used anywhere in a script (not just in the chunk of code it is defined in). Consider this:

a = "apple" --global `a` is "apple"

function doSomething()
	local a = "banana"
	print(a) --> "banana"
	a = nil --sets the local variable to nil
	print(a) --> "apple" (using the global variable)
end

doSomething()

The 'while' Statement

The while statement is another kind of loop. The block of code will be executed as long as the condition between 'while' and 'do' is neither nil nor false. When using true as the condition, you should always use wait() in your while statements, else ROBLOX will freeze and crash!

Example one:

while true do --this repeats forever since "true" is always neither nil nor false
	wait(3)
	print("Hello World!")
end

We can also instead of putting the wait after while true do, inside it, since wait never returns nil or false!

For example:

while wait(3) do
 print("I waited three seconds, and this message came up")
end

Another example:

energy = 0
while energy < 10 do
	wait(3)
	print("Charging up...")
	print(energy)
	energy = energy + 1
end

Example one shows a function that will never end. Example two shows how a condition is tried each time the loop ends.

The 'for' Statement

The 'for' statement is another type of loop. There are two kinds - generic and numeric. One works with tables and iterator functions, and the other with numbers.

Numeric for is generally considered easier than generic because of it's simplicity. The format for numeric for statements is:

for a = b, c, d do
	--code
end

Lua first defines a local variable `a` as `b` (`b` must be a number). It will then execute the code, and add `d` to `a`. It repeats the process until `a` is greater than or equal to `c`. You can leave out `d` and Lua assumes 1 as d.

Example one: (numeric for)

for i = 1, 10 do
	print(i)
end

This executes the block of code 10 times, incrementing i by 1 each time until it is 10. Notice how the delta amount (the amount that is added) is omitted and Lua assumes 1.

Generic for statements are a bit more difficult because they use iterator functions. Iterator functions give you the next value in some kind of list each time they are run. Lua runs the function until the iterator function does not give a value back. That's really complicated to know. In Lua, you can use the pairs() function right inside the for statement to generate an iterator function for that table.

This is a lot easier done than said. Consider this example.

Example Two:

a = {"Hello", true, 1337}
a.derp = "herp"
for key, value in pairs(a) do
	print(key, value)
end

1 Hello 2 true 3 1337

derp herp

This runs the chunk of code for every value in the `a` table.

Generic for-statements can be used to easily go through the children of an object:

Example Three:

bricks = 0
a = Workspace:GetChildren()
for k, v in pairs(a) do
	if v:IsA("Part") then
		bricks = bricks + 1
	end
end
print("Bricks: " .. bricks)

See also