Flurite's Scripting Tutorials: Difference between revisions
>Flurite New page: __TOC__ ==Prologue== Hello new scripters! In this tutorial, you are going to learn the very basics of scripting! To start out, ROBLOX uses a language modified to their liking, known as R... |
>Flurite New page: __TOC__ ==Prologue== Hello new scripters! In this tutorial, you are going to learn the very basics of scripting! To start out, ROBLOX uses a language modified to their liking, known as R... |
(No difference)
|
Revision as of 20:59, 4 August 2011
Prologue
Hello new scripters! In this tutorial, you are going to learn the very basics of scripting! To start out, ROBLOX uses a language modified to their liking, known as Rbx.lua. Mastering this language will give you the ability to make planes, guns, and even a fun GUI-game for your place! Lets start off nice and easy right here. Just so you guys know, this beginners "tutorial" is not supposed to be read in a short amount of time. Scripting takes months if not years to become good at, so maybe take a section at a time.
Information you need to know
- First of all, you will need to know the ins-and-outs of ROBLOX Studio. If you don't already, visit this article:
- In this tutorial, there will comments, which are designated by using "--" characters. This text is just for you to understand what a specific part of my script means.
Inserting a Script
There are quite a few ways to insert a script in to your game, but since you are most likely a beginner, this is the easy way:
1. In the Explorer menu, click on Workspace
2. Go to ROBLOX Studio, and at the very top of the window, you should see a line of buttons which include "File", "Edit", "View", "Insert". Click the button "Insert" and you should see a drop down menu.
3. From the drop down menu, click "Object..."
4. You should see a small window pop up with a lot of options, find the option, "Script", and double click that! You should see an object in your Explorer menu called "Script"!
Congratulations, you inserted your first script!
About the ROBLOX hierarchy
If your new to scripting, you might not know what a hierarchy is. I'll define it for you:
"A system or organization in which people or groups are ranked one above the other according to status or authority."
In ROBLOX Studio, the 'game' DataModel is the uppermost section of the hierarchy, and everything in the game is part of the 'game' hierarchy. It is the keystone of ROBLOX's structure. In this section you will learn two terms: 'descendants' and 'parents'. In Rbx.lua, a parent is what contains the object, so most objects have only one parent (besides the 'game' DataModel, which has known).
For example, when you insert a brick, it usually appears in Workspace. Therefore, the new brick's parent is Workspace! A descendant is the exact opposite of a parent.
For example, when a new brick is inserted in to Workspace, the new brick is a descendant of Workspace! There is also a term known as 'ancestor', which is similar to a parent, but goes higher up the hierarchy then the regular parent. It's kinda like a parent's parent's parent, or parent's parent's parent's parent, etc.
Getting through ROBLOX's hierarchy
Now that you know all the terms, I will teach you HOW to move through ROBLOX's hierarchy. It's actually quite simple, and all you need to do is use a '.' (period) character! Since you probably now know that Workspace is a descendant of the 'game' DataModel, then you can access Workspace by simply going like this:
game.Workspace
Simple, huh? Since ROBLOX realized that Workspace was so commonly used, they made shortcuts to it. So to access Workspace you can type any of these options:
- game.Workspace
- Workspace
- workspace
P.S. You cannot use these shortcuts for any of the other direct descendants of the 'game' DataModel, like StarterPack and StarterGui.
Now to access a brick in the Workspace, you can simply use another '.' (period) character!
game.Workspace.Brick
Make sure you use the name of the object, not the className. Now you can move anywhere through the ROBLOX hierarchy!
Variables
Most of our newer scripters who have not gone through math classes such as pre-algebra, may not know what variables are. Variables, also known as 'tags of an object', are simply a symbol or string of characters that represents a value! Even if you have not gone through pre-algebra, you probably know how to solve a problem like this:
a = 5
a + 2 = ?
And you would probably know that a + 2 is 7. But what you might have not known is that 'a' is a variable! In this case, 'a' is the variable, and as you may be able to guess, '5' is it's value! In Rbx.lua, variables are setup almost exactly the same! In fact, if you were to put a similar piece of code in a script, it would work! Here's an example of a code:
a = 5 --This is similar to the variable I gave you. This is also an example of a comment. print(a + 2) --when I say print(a + 2), I want it to print what's inside the parenthesis to the Output window. Check your output window to see what it says! Output: 7 --see, how it prints the result of 'a + 2'?
If copy and paste everything up to the second line of the code, you will see the result in your own output!
Congratulations, you have learned what variables are! If you have trouble grasping variables or the print() function, see the following links:
Operators
Here are some key operators that you may want to know when using ROBLOX. This can be used as reference, but if you read through the whole thing, you won't lose anything from it.
Math Operators
- '=': This operator is used to assign variables, as you can tell from the last section.
a = 5 --this is a variable
- '+': This operator is used to add number values.
a = 5+2 print(a) >7
- '-': This operator is used to subtract number values.
a = 5-2 print(a) >3
- '*': This operator is used to multiply number values.
a = 5*3 print(a) >15
- '/': This operator is used to divide number values.
a = 6/2 print(a) >3
- '%': This operator is used to divide number values with a remainder.
a = 5%2 print(a) >1
- '^': This operator is an exponent operator.
a = 5^2 print(a) >25
Comparison Operators
In this sub-section, I am going to be using 'if' statements. You will learn about those in the next section. For now, just read it like if it were a regular sentence. Ex: In the first example, read it like this: 'if a IS 5 then', and so on.
- '==': This operator states that one value is equal to another.
a = 5 if a == 5 then print('a is 5') end >a is 5
- '~=': This operator states that the two values are not equal.
a = 5 if a ~= 2 then print('a is not 2') end >a is not 2
- '>': This operator states that the first value is greater than the second.
a = 5 if a > 2 then print('a is greater than 2') end >a is greater than 2
- '<': This operator states that the first value is less than the second.
a = 5 if a < 7 then print('a is less than 7') end >a is less than 7
- '>=': This operator states that the first value is greater than or equal to the second value.
a = 5 if a >= 5 then print('a is greater than or equal to 5') end >a is greater than or equal to 5
- '<=': This operator states that the first value is less than or equal to the second value.
a = 5 if a <= 6 then print('a is less than or equal to 6') end >a is less than or equal to 6
Logical Operators
In Lua, there are 3 logical operators, and, or, and not. Here is a summary of each one.
- And operator: This operator is used to check if both values are true.
a = "Pie" b = 1 if a == "Pie" and b == 1 then --notice how I use two equal signs to designate that one variable IS this value. print('a is pie, and b is 1') end >a is pie and b is 1
- Or operator: This operator is used to check if any of the values are true.
a = "Apple" b = 123 c = "Banana" if a == "Apples" or b == 123 or c == "Bananas" then print('One of these is true, can you figure it out?') end >One of these is true, can you figure it out?
- Not operator: This operator is used to find the opposite of a boolean. The opposite of true is false. The opposite of false is true.
a = true for i = 1, 5 do a = not a --it changes a's value to it's opposite. print(a) end
Congratulations, you have completed the operators section! Now you can compare different values with comparison operators, make your code more efficient with all logical operators, and do some math with some mathematical operators! If you still don't get it, you can visit this article:
Conditional Statements
In some of the previous examples in the section before, you probably saw some 'if' conditions. Like when I did:
if a == 5 then --code end
In this section, you will be learning about 3 of the major conditional statements of Lua, the 'if' statement, the 'elseif' statement, and the 'else' statement. This is simple guideline on how you should use these, but I will go more in depth about them later:
- if statement -use this statement to execute some code only if a specified condition is true
- if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false
- if...else if....else statement - use this statement to select one of many blocks of code to be executed
If condition
An 'if' condition simply does what it says: check if a value is another value. If conditions always need an 'end' statement to close the condition. This is the setup of an 'if' condition.
if (something == value) then
It's quite simple after you get the hang of it. Here's an example with an 'if' statement. Notice how I use two '=' signs as that designates that something IS another.
abc = 123 if abc == 123 then print('abc is 123') end >abc is 123
And another example:
abc = 123 laugh = "lol" if laugh == "lol" and abc == 123 then print('It worked!') end >It worked
If..elseif Statement
Use the if..elseif statament to execute a piece of code if one of the given conditions is true. The format is much like the 'if' statement. Here's an example:
a = 3 if a == 1 then --I begin with my 'if' statement, because you can't use an 'elseif' statement if there is no corresponding 'if' statement. print('Hi') elseif a == 2 then --See how the format of the 'elseif' statement is very similar to the 'if' statement's format? print('Hello') elseif a == 3 then --You can use multiple 'elseif' statements in your piece of code. print('Hey') end --notice how I only need one end to close all my 'if' and 'elseif' conditions. Output: Hey
Like the 'if' statement, you can use the 'and' and 'or' logical operators in your piece of code. Here's an example with that:
a = 3 b = 2 if a == 2 and b == 3 then print('Flourine') elseif a == 3 and b == 2 then print('Flurite') end Output: Flurite
If..elseif
Use the if....else statement to execute some code if a condition is true and another code if the condition is not true. Here's an example of using this:
a = 3 if a == 5 then print('"a" is 5') else --there is no written condition for the 'else' part. If the 'if' statement isn't true, then it automatically executes the 'else' statement print('"a" is not 5') end --notice how I use an end to close this type of statement Output: "a" is not 5
Combinations
All these types of combinations can be combined. Here's an example:
a = 3 if a == 1 then print("1") elseif a == 5 then print("5") else print('"a" is neither 1 or 5') end
Creating objects via script
In Rbx.lua, you don't have to use the insert menu to get a brick on to your game, but instead you can always use a script. You can simply use the Instance.new method to create any object that exists in Roblox. The method has two arguments, the first being the className of the object you are setting, and the second being where the new object is going to be parented. Here's an example of using Instance.new:
Instance.new("Part", game.Workspace) --this creates a new brick (className is part), and it is parented under Workspace
If you run this script, you should see a new part in Explorer. You can also change properties of the new part you created by tagging it first.
brick = Instance.new("Part", game.Workspace) --I create the new brick and parent it under Workspace. I also, tag it as the variable 'brick'. brick.Name = "Brick" --I change the newly created part's name to "brick". brick.Anchored = false --I change the 'Anchored' property of the brick to false --you can edit all it's properties if you wanted