generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: HOW THE HECK DO I SCRIPT?

Previous Thread :: Next Thread 
Porkina is not online. Porkina
Joined: 12 Mar 2012
Total Posts: 8
11 Jun 2013 11:50 AM
I understand like nothing about Roblox Studio, much less how to script

A little help here?
Report Abuse
JayvulMaxtros is not online. JayvulMaxtros
Joined: 03 May 2009
Total Posts: 266
11 Jun 2013 11:52 AM
Experiment with free model scripts and break them down line by line - then try and figure out what those lines mean.

www.wiki.roblox.com is also a great help.

Output is also a scripter's/beginner's best friend. c:
Report Abuse
Darthnoodles98 is not online. Darthnoodles98
Joined: 02 Apr 2011
Total Posts: 418
11 Jun 2013 11:52 AM
I'm not great at scripting myself, but if you want to get into it, I suggest you get ROBLOX Basic Lua Programming by Brandon LaRouche, or just look at wiki.roblox.com.
Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
11 Jun 2013 11:55 AM
What do you need? Even if you haven't tried scripting, before, we can give you an easy-to-understand explanation of what you could do to in order to achieve your goal.

If you want to start learning RBX.Lua, though..
I would recommend grabbing a simple free model, such as a VIP door, and reading the script.
Read it until you understand it. If there's something, there, that you aren't familiar with and can not figure out, then feel free to either ask it here or check on http://www.wiki.roblox.com and figure it out.

http://wiki.roblox.com/index.php/Table
http://wiki.roblox.com/index.php/Variables
http://wiki.roblox.com/index.php/Function
http://wiki.roblox.com/index.php/Loops
http://wiki.roblox.com/index.php/Conditional_statement
http://wiki.roblox.com/index.php/Methods
http://wiki.roblox.com/index.php/Arguments_and_Parameters
http://wiki.roblox.com/index.php/Operators

These are some of the basics that you can use to learn what things mean, do, and are, so that you can use them to create your own things.

Another good way to assist in your learning is to check out some of the threads on this sub-forum, so you can learn from others' mistakes and successes.
Report Abuse
LifeIsUnfairReturns is not online. LifeIsUnfairReturns
Joined: 10 Mar 2012
Total Posts: 4612
11 Jun 2013 12:02 PM
Here is a scripting tutorial I wrote

--[[
==The Fundamental Roblox Lua Scripting Guide==

--------------------------------------------------------------------------------------------------
Table Of Contents
--------------------------------------------------------------------------------------------------

I-----Introduction------------------------------Line 0025
II----About Lua---------------------------------Line 0075
III---Roblox Studio Tools-----------------------Line 0120
IV----Syntax, Comments, Planning, & Keywords----Line 0241
V-----Variables, Data Types, & Object Tagging---Line 0312
VI----Arithmetical Functions--------------------Line 0482
VII---Statements & Commands---------------------Line 0584
VIII--While True Do Loop------------------------Line 0744
IX----If Statement------------------------------Line 0825
X-----Basic Functions---------------------------Line 0988
XI----Arrays/Tables-----------------------------
XII---For Loop & While Loop---------------------
XIII--Instance----------------------------------
XIV---GUIs--------------------------------------
XV----Index & Glossary--------------------------

--------------------------------------------------------------------------------------------------
I - Introduction
--------------------------------------------------------------------------------------------------

-----Hello world!-----

Welcome! You know how annoying free models are in games. They lag and people criticize you for
using them. Aren't you tired of using them in your places?

Or rather, have you been wanting to add that feature to your game that is so special, you
can't find a fitting free model?

Then read this beginners' guide for Roblox Lua. It will teach you the basics, which can cover
anything from button doors to creating objects, and even conditional events and loops.

-----What is scripting?-----

Computers don't run by themselves. They must be told what to do and when to do it. This is
accomplished by scripting. Computers don't understand English; they only understand a certain
type of language. There are many scripting languages, but Lua is the one we will be covering
in this course. So basically, scripting is telling the computer what to do.

-----Who this guide is written for-----

I believe anyone can learn Roblox Lua easily. This guide is written for both the fast learner and
the slow learner. Everything is explained in simple terms, and sample scripts are provided.
Lua is also easier to learn if you have previous experience in computer science or programming,
or you have been editing free model scripts. But enough editing, it's time you started making
your own scripts from scratch.

-----What's featured in the guide-----

First, in chapter II, this guide gives a bit of information about Lua, and informs you about
what kind of scripting language it is. Next, read chapter III if you are having trouble with
Roblox Studio, because this covers some special features in Roblox Studio that can help you
with your scripting experience. Afterwards, the basics are covered, and later, more advanced
and powerful scripts are explored.

At the end of each chapter, you get to take a quiz so you may test your understanding of each
subject. You may skip these, but the only way to get good at scripting is practice! Submit
your answers to me via PM when you finish the quiz; I will grade it. The quizzes are
located in the other script titled "TheTests".

In the end of the guide is an index for scripts and terms, and also a glossary.

It's better to learn this over an extended period of time with practice rather than try and read
this all in one day. You won't remember anything if you try to rush this and aren't patient. But
you'll find that scripting is not hard work; rather, it is actually fun and addictive. And
you will feel so much better using a script you made 100% your own rather than a free model.

--------------------------------------------------------------------------------------------------
II - Lua
--------------------------------------------------------------------------------------------------

-----What is a scripting language?-----

Originally, computers could only understand something called "machine code". Basic machine code
is very lengthy, and not easy to work with. So scripting languages were developed. Each scripting
language is different, and has different grammar. Some are easier to learn than others. Some use
more code than others. After you have written in a scripting language, a machine called the
"compiler" turns the much shorter scripting language code back into original machine code.
The computer only really understands true basic machine code. Fortunately for us, the compiler
allows us to use simpler code!

-----Roblox & Lua-----

Lua is the scripting language used by Roblox. However, Roblox doesn't have the original Lua; it
features a slighty edited version (I'll go into detail on this later). Roblox allows you to
create objects called "Scripts". If you double click on any script, it opens up a page where
you can write in Lua (just like this one). Script objects can be placed inside everything.

-----The tech of Lua-----

Lua is an embeddable, or extensible language; that is, you cannot write a program 100% in Lua.
A program cannot be launched with Lua. However, you can embed, or insert Lua into your program,
then write the rest of it in Lua. Lua is also a lightweight programming language, meaning that
it takes very little code to write scripts. It's a very good language for beginners to learn.

-----Some interesting facts about Lua-----

Lua was developed in 1993 mainly by Roberto Ierusalimschy, who still maintains the language to
this day and has written several books on it.

A recent survey showed that Lua is the most popular scripting language used for the purpose of
writing video games. World of Warcraft is written in Lua. Garry's Mod is written in Lua and has
Lua scripting capabilities like Roblox. Also, "Lua" is portuguese for moon. Don't capitalize it!
It doesn't stand for anything. This gets forumers on Roblox mad.

See more at their website, www.lua.org

Their website features its own tutorials, but they are written for the experienced adult rather
than the starting learner. It assumes you have already learned some other scripting language.

(There is no quiz for this chapter.)

--------------------------------------------------------------------------------------------------
III - Roblox Studio Tools
--------------------------------------------------------------------------------------------------

-----What tools will you be using?-----

Roblox Studio is the program in which, of course, you will be writing Lua and creating your game.
The 4 main tools that are helpful when scripting are the Explorer Box, the Properties Box, the
Command Bar, and Output. You'll need to familiarize yourself with these tools because concepts
in the course require knowledge of each one of these. We will cover them in detail. Even if you
think you are already pretty familiar with Roblox Studio, please read this because there are a
few things you may not know.

(WARNING! You cannot script during play mode! If you haven't already, you should copy this
script into an empty place in Roblox Studio. This is a good reason why you should learn to work
with Roblox Studio if you haven't already, because you will be using it rather than play mode.)

-----Explorer-----

The explorer window should be on your screen to the right. If it isn't, go up to the menu above,
click on "View", and click on "Explorer". It should appear.

Explorer is basically a list of all objects inside the game. Every brick, script, model, and
folder is listed inside this box. To understand this feature better, we must explore the concept
of hierarchy.

-----Hierarchy-----

Hierarchy is the order, or arrangement of all objects in the game. Some objects are on "higher"
levels than others, some are "below" others, and some are on the same level. For example, every
object inside a folder is at the same level. A script inside a brick is at a lower level than
brick it is inside, and the brick is "above" the script. Hierarchy is an important concept to
understand when scripting because we are required to locate our objects in our scripts. Explorer
is a good way to get an idea of where exactly your brick is located.

-----Exploring Explorer-----

Exploring the Explorer box is rather simple. Inside "game", the highest folder in the hierarchy,
we have the folder called "Workspace".
Every visible object is placed inside the folder Workspace. So to view objects inside Workspace,
you must double-click on it or click the plus beside it. Objects will sometimes have objects
inside them such as scripts, so to go down a level in the hierarchy, simply double click on any
object/folder. From here, you may also select bricks, and you can copy and delete many objects
using Explorer too. You can tell if an object has something inside it by looking to the left. If
there is a small plus to the left, click on it to reveal any extra objects inside the selected
object.

Warning: if you have a few thousand objects in Workspace, opening this folder in Explorer will be
extremely laggy. It's a good idea to create models so you can better organize your game and reduce
lag in the Explorer box.

-----Properties-----

The Properties box should be just below your Explorer box on the right side of the screen. (If it
isn't, activate it through the "View" menu the same way as Explorer.) Perhaps you've noticed that
whenever you select a brick, folder, or model, a ton of data and other stuff appears in this box.

You know what a property is, right? It is an attribute, a detail of something, a special quality.
Through this box we can edit the properties of anything we select in its counterpart, Explorer.

You can edit most of the data that appears. For example, you can change the name of any brick or
model you create through here by going to the line "Name" and changing the text in there. You can
change the color of a brick, the size, the position, everything. It's good to look through all
of the properties of a brick and memorize the important ones. Some properties are locked, however.

A large part of scripting is changing the properties of bricks. For example, a script that makes
a door open is really just changing the Transparency property and CanCollide property. It would
be a good thing to learn the meaning of properties which you haven't explored or understood yet.
You can do this through experimenting or messaging me. The object browser which can be found
in "Help" is also interesting. This list I made covers the first few basic properties:

DATA
ClassName - This is the type of object you're viewing.
Name - The name of the object.
Parent - Think back to hierarchy. This is the object "above" the object you're viewing.
Position - The physical location of the brick in game.

BEHAVIOR
Anchored - If this is checked, the brick cannot move by physical means.
CanCollide - If this is checked, you cannot walk through the brick.
Locked - If this is checked, the brick cannot be selected through anything but Explorer.

-----Command Bar-----

(If it is not visible, go into view, go down to toolbars, and click "Command". Then go
to the bottom right corner of the screen and locate an arrow pointing downwards; click on it,
highlight "Add or Remove Buttons", then "Command", then click command.)

Perhaps you've heard of a process called CFraming. It allows you to rotate blocks in ways you
couldn't before, allowing for complex structures or realistic terrain. Most use a CFrame tool
in game, but some use the command bar. The command bar is a long bar at the bottom of your
screen. You can enter Lua commands into this toolbar and use them to edit your game realtime
without having to hit play. Through here, you can CFrame as mentioned above, and insert objects
into folders that you couldn't before (this can actually be useful sometimes).

While I prefer using a CFrame tool anyway, you'll enter and test most of your commands through
here once you have learned basic commands.

-----Output-----

Most of the time, your script won't work on your first try. Usually, we can fix the error in the
code, but when you cannot find an error, that's ok. If you run the program with the output window
open, output will tell us the error in your script. For output to print anything, it must be open
when you enter play mode. Since output is a pretty big window, I prefer to keep it closed right
up until I am going to test a script. If any error message is going to come up, it will appear
in red text and show the name of your script along with the number of the line the error is on.

Occasionally, output cannot detect an error and therefore, print nothing about your broken script.
If your script is not working and output is not showing any error messages, only then do you need
to go to the scripting helpers forum and ask for help.

Output doesn't only print error messages; we can use output to print the results of scripts that
we create; so the use of output will be required.

Now, it is time to take your first quiz! Locate "Quiz 1:Roblox Studio & Lua" in the quizzes script
and send your answers to me for grading via PM.

(If you have any questions about this chapter, please message me with your question.)
-xXAlphanumericXx.

--------------------------------------------------------------------------------------------------
IV - Syntax, Comments, & Keywords
--------------------------------------------------------------------------------------------------
(This is the chapter where I placed all of the stuff that didn't really fit anywhere)
-----Use Proper Grammar!-----

As stated before, computers cannot read English. So when writing, each script you learn will
have something called "Syntax", which is another word for the grammar, or setup, of each script.

Just in case you skipped the introduction earlier, basic machine code is the only thing that
computers understand. Writing in it, however, is very time consuming, and takes a lot of code to
create simple scripts. So scripting languages were developed, and a machine called a "compiler"
reads the simpler scripting language codes and turns it back into basic machine code. So
when you compile a program, you basically run it.

Once you have learned the basic syntax of any script, you'll find it easy to write scripts over
and over from memory. When your syntax is wrong, errors occur because the compiler either
expected something different or something is completely out of place.

A good thing to know is that the compiler reads the script in order; it reads from line 1 to
the end of the script. That is, from top to bottom. If the compiler reads an error, it will
NOT pass the line the error is on until the error is resolved.

-----Keeping Things Organized: Comments-----

When scripting, you can get caught up in your code and even get lost. I find comments an easy,
quick way to organize your program. The syntax for creating a comment is also easy. To create a
single comment, put two dashes in front of your sentence, like so:

--This is a comment

The text will turn green if you've done it correctly. The compiler ignores any text in a
comment, so you can use this to mark certain pieces of the code, or provide important info for
later. You may have noticed this entire guide so far is a comment, but yet there aren't two
dashes before every line. That is because this is a multi-line comment. To create a multi-line
comment requires two dashes then two square brackets, like so: ]]--

--[[
Multi line comment!
]]--
--[[
You must also include the second part to end the comment. If you don't end the comment,
Output will give you an error message. It won't break your script, but it's annoying when output
gets clogged up with big red error messages.

----- Keywords: Those Big Blue Fancy Words -----

Perhaps while editing a script, you noticed a word was colored blue. There are only a few
specific words that turn blue like that. Why? These words are called "keywords". They are
words that are reserved for a special purpose. You can't use them other than when they are
meant to be used. Keep this in mind as you script.

Now time to actually begin scripting!

(As always, message me with your questions!)
(There is no quiz for this section)
-xXAlphanumericXx
Report Abuse
LifeIsUnfairReturns is not online. LifeIsUnfairReturns
Joined: 10 Mar 2012
Total Posts: 4612
11 Jun 2013 12:03 PM
There is more

Ignore the --[[ things


--------------------------------------------------------------------------------------------------
V - Variables, Data Types, & Object Tagging
--------------------------------------------------------------------------------------------------

-----Variables-----

Let's review the concept of a variable. By definition, a variable is something that can be
changed; and this is true for scripting. A variable is given a name, and it holds a value.
It is basically a placeholder.

For example, we might create a variable called "A", and give it a value of 5. (If you have
experience with Algebra, you can understand a variable as something like "x" in an algebraic
equation, but here we get to choose the value rather than try to figure it out.) So if we have
a variable called A, and we say it is equal to 5, the computer reads "A" as 5 the next time we
talk about, or reference "A". If we change the value of "A" to "Cat", then the computer reads
A as "Cat". Simple enough?

-----Declaring a Variable-----

The syntax for declaring a variable is really simple and only takes 1 line of code. Here it is:

VariableName = Value

Here, "VariableName" is the name of our variable, which can be almost anything. "Value" is the
value which you have chosen to give to the variable. Here's an example: ]]--

A = 10
--[[
(Note, numbers always show up as a light blue)
So you see that as earlier, A is now known to the computer as 10. So if we told the computer to
write "A", it would write "10" in Output. Now, be warned, there are rules to declaring variables.

-----Rules for Variables----- <-------------- YOU WERE HERE RILEY

*Lua is a case sensitive language. "A" is not equal to "a". So if you made a variable called "HI"
and then referred to it as "hi", the computer would error because it couldn't find "hi". Watch
your capitalization! This rules actually goes for all scripts. Watch caps everywhere!

*You cannot have two variables of the same name. I think that if you try and declare the same
variable twice, it actually just changes the value from when it was declared the first time.

*A variable name cannot start with a number, or be 100% numbers. "1A" won't work as a variable
name, but "A1" will.

*A variable name cannot have spaces in it.

*A variable name cannot be the same as a keyword (see line 296 for a refresher).

-----Data Types-----

Now, in Lua, we don't have to do much about data types. But it is still important to discuss and
review these. A data types is pretty much exactly what it means: a type, or form, of data.
When we declare a variable, we don't have to define the data type (You usually have to in
other languages) but it is important to know the basic data types:

*Integer
The most basic kind of data, This type of data is numbers that don't have decimals. 400, 9, and
-23 are integers. Your number of KOs is an integer.

*Double
Another kind of number data, but this type holds numbers with decimals. 32.56, 20, and -23.1 are
doubles. Doubles, however, do not have as far of a range as decimals do.

*Boolean
Although this is technically a number data also, it's better described as true or false. Boolean
data holds 1 and 0 only. 1 is true, and 0 is false.

*String
A string is a type of data that holds characters (letters). It has to have a pair of quotation
marks around it. Examples of strings are "Hello", "This is a string", and "123 ABC".

-----What's the point of a variable?-----

You're probably thinking, "Why don't I just say "10" instead of going through the trouble of
declaring a variable?"

Well, first, variables don't only have to point to values. Variables can be used to reference
bricks and models, and using a variable cuts down on code a lot when doing that. Second, there
are some functions and scripts that simply require a variable to be used. Besides, declaring a
variable is no hard work. It's very simple, right?

Now that you're familiar with variables, let's explore the process of Object Tagging.

-----Object Tagging-----

What is Object Tagging?
It's when we say that a variable is equal to an object, rather than a value. The syntax of object
tagging is just the same as declaring a variable and setting it to equal a value. Here it is:

VariableName = (Locationofbrick).(BrickName)

Now, where exactly is the brick we want to find? Review the Explorer Window and Hierarchy section
in Chapter 3 and see if you can guess.

-----The Location of Everything-----

First off, everything you see is located inside a folder called "game". You cannot see this
folder from Explorer but it does exist. So first comes game: ]]--

A = game --[[

Remember that A is going to be our variable. We are setting up A to equal the brick itself.
A step "below" game is Workspace. So next is Workspace. Watch your spelling and capitalization,
and separate the words by only a period. No spaces here: ]]--

A = game.Workspace --[[

Now, let's say the brick is inside Workspace (like most are.) Now that we have found the
location, we enter the name of the brick. To see the name of a brick, you can check the "Name"
property in the properties box or simply view it in the Explorer. Let's say the name of the
brick is simply "Brick". ]]--

A = game.Workspace.Brick --[[

We have successfully "tagged" the brick! In other words, A is now equal to "Brick". Now,
instead of having to write "game.Workspace.Brick" over and over, we can now just simply write A.
Take note of how the hierarchy works. For every period we pass to the right, we go "down" a level
in Roblox's hierarchy. So imagine our brick is inside a model. We add in the model between
Workspace and Brick. ]]--

A = game.Workspace.Model.Brick --[[

Not so hard, is it? Now that you have tagged the brick, you can do anything you want to it
just using "A" instead of all of that extra, unneeded code. The understanding of hierarchy helps
you to understand searching for objects better.

Now, there is another method of object tagging which can be much shorter. This was the part
added by Roblox. It is the "Parent, Child" system of finding objects, and if your object is far
down below in the hierarchy, it is much better to use.

Review the concept of a parent and a child. The system works the same way hierarchy does:

*A parent of a brick is the folder or model the brick is inside.
*A child is a brick/object inside a folder or model.

For example, look at a brick in Workspace. The brick is the child of Workspace, and the Workspace
folder is the parent of the brick. So if you say "Parent", it means up one level, while "child"
means down a level. Let's take a look at a variable tagging the brick it is inside: ]]--

A = script.Parent --[[

As you know, the script is the child of the brick, and the brick is the parent of the script.
So the compiler knows that "Parent" is referring to the brick the script is inside. You don't
need to include the brick name unless you reach out further than just 1 parent.

For example, say there's a script inside "Brick1", and "Brick1" and "Brick2" are inside a model.
Our goal is to reach "Brick2". What do we do?

We must add two parents to reach the model, and then specify the brick we want to find/tag inside
the model. Take a look right here: ]]--

A = script.Parent.Parent.Brick2 --[[

Here, the first parent is the brick, and the second parent is the model. Brick2 refers to the
brick inside the model. Got it? Take note that you always start with "script" then start
traveling through the hierarchy. "script." is not the name of the script you're writing in;
it shouldn't be the actual name of your script. Separate with periods only, and watch spelling!

That's all there is to object tagging, variables, and data types (at least, all that you need to
know)! Now that we have defined a variable, let's start performing some mathematical functions.
Here, we will finally get to see some output and results!

Now, it is time to take your next quiz! Locate "Quiz 2:Variables & Syntax" in the quizzes script
and send your answers to me for grading via PM.

(Again, now that you have begun writing code, you will likely have many questions. I know this
guide cannot answer them all. Please message me with any questions you have on any subject!)

-xXAlphanumericXx

--------------------------------------------------------------------------------------------------
VI - Arithmetical Functions
--------------------------------------------------------------------------------------------------

-----Math is Fun!-----

An arithmetical, or mathematical function is simply performing any sort of operation with numbers.
Adding two numbers together is an arithmetical function. Multiplying, dividing, subtracting, they
are all mathematical functions. In this chapter, we will discuss how to perform such mathematical
functions in Lua itself, and finally get to see output from our scripts! Although it may not be
very fun to some, you have to learn these math functions because they are an essential part of
scripting itself. Basic algebra is very good to know for this section, but it is not required.
First, let's see how to use the print function so we get to see something happen in output.

-----Print Function-----

Print, maybe the simplest of all non-arithmetical functions, simply tells the compiler to print
what comes after it in output. The syntax for a print function is: ]]--

print "whatever you want to print goes here" --[[

This is actually a working statement here. "print" is all you need to say for the computer
to understand it must print the following text. Note that the program is printing a string.
If you are printing something other than a string, such as a variable or number value, you must
use parentheses. In fact, I just think it's easier to use parentheses everytime you use print
so you won't forget. Here's an example: ]]--

print (10)
--[[
Now, remember that neither numbers nor variables are put in quotation marks. Only if you wish to
print strings, should they be added. Also, take note that strings cannot be used to perform
math operations. "1" + "2" will error.

-----Operators-----

Before we get to the math, let's discuss operators. Operators are basically symbols that perform
some sort of operation (thus, the term "operators"); for example, the = sign is an operator.
You always find operators placed between two values, or variables. Here is a list of the
operators which we will cover in this section:

+ *The addition operator, tells the compiler to add
- *The subtraction operator, tells the compiler to subtract
* *The multiplication operator, tells the compiler to multiply
/ *The division operator, tells the compiler to divide

-----The Code-----

There are two methods to performing a mathematical operation and printing the result. Here's the
syntax for the first method, which is shorter and simpler:

print (Value1 + Value2)

Here, the print function simply prints the sum of Value1 and Value2 using the addition operator
in the middle. Here's a working example: ]]--

print(2+2) --[[

The output will be 4. The second method of math operations is a bit more complex but definitely
much more powerful; it implements the use of variables. Here's the basic syntax:

Variable = Value1 + Value2
print (Variable)

Why is this method better? It's more code. Here's why: Imagine we define 2 variables, and let the
user input them. Then we can actually make something close to a calculator without the user
having to edit a script. Also, you can use the same variables to perform all of the other
mathematical operations to keep code short. Here's a working example: ]]--

Result = 50 + 22
print (Result) --[[

All four basic mathematical operations have the same syntax, but what changes is the operator.
Here's an example of where there are two variables that perform all four mathematical operations: ]]--

A = 20
B = 10
print (A + B) -- Returns 30
print (A - B) -- Returns 10
print (A * B) -- Returns 200
print (A / B) -- Returns 2
--[[
So now that you have learned all four basic math operations, we can put these together to perform
more advanced Algebra formulas. You can literally have a computer do homework for you this way.

Let's use a basic example: to calculate the area of a cube, all you need is the length of any one
side. The formula for finding the area of a cube is L * L * L, where L is the length of a side.
Now, let's complete this problem using scripting rather than our brains! ]]--

L = 5
print(L * L * L) --This is why any number multiplied by itself 3 times is "cubed"
--[[
This returns 125, the correct answer. Because of the use of a variable, every time that we want
to calculate this is easier because we only have to enter 1 number rather than 3 numbers. Now,
let's use these basic math functions to edit the properties of bricks!

Now, it is time to take your next quiz! Locate "Quiz 4:Math Functions" in the quizzes script
and send your answers to me for grading via PM.

(Message me any questions!)

-xXAlphanumericXx

--------------------------------------------------------------------------------------------------
VII - Statements & Commands
--------------------------------------------------------------------------------------------------

Would you like to make a brick dissappear right in front of your eyes? How about making a brick
extremely shiny? Perhaps, you want to set a brick up really high and then make it fall?

We can do all of that, and we don't even have to enter test mode. By using the command bar,
we can perform commands that change the properties of bricks; and this is a big part of
scripting itself. These sort of commands require the understanding of hierarchy, the boolean
value, and math functions. It would be good to review these before starting this section.

Also, we will use the command bar. (If the command bar is not visible, see line 205.)
The command bar, the long bar at the bottom of the screen, is where we will be entering our code.

----- Locating A Property -----

All properties of any object are basically inside them; so finding the properties is pretty easy.
As you know, properties hold a variable, and based on this variable, the brick changes that
property. The syntax for a statement like this can be different based on what you want to do with
the value. If the property holds a boolean value (True or false), here is our syntax:

(Location of brick).(Brick name).(Property) = (True or false)

Here, once we have found the property, we set it to equal either true or false using the "="
operator. Take a look at this example statement: ]]--

game.Workspace.Brick1.Anchored = false
--[[
Try it out! Create a brick, set it up very high, then anchor it. Hit the play button above at
the top of the screen and enter this command into the command bar (make the sure name is unique
and matches!). Press enter, and watch the brick fall. *Don't hit test mode. I mean the green
arrow at the top of the screen. Click reset when you're satisfied.

Changing properties with number values is easy too. Take a look at this example script: ]]--

game.Workspace.Brick2.Transparency = 1
--[[
This command sets the transparency of the brick to equal 1. You can play around with these
commands using the command bar, but if you left a command like above in a script and went into
test mode, it would also work.

----- Performing Mathematical Commands with Properties -----

Adding or subtracting a value from a property is a bit harder than just setting the value of
the property itself. You must say that the property's value is equal to itself plus whatever
you want to add (or minus what you want to subtract). Here's the syntax:

(Location of brick).(Brick).(Property) = (Location of brick).(Brick).(Property) + (Value)

Seems a bit complicated but it is very simple after a bit of practice.

Here's an example script that adds 0.5 to a brick's transparency. ]]--

script.Parent.Transparency = script.Parent.Transparency + 0.5
--[[
Variables are really helpful in simplifying situations like this. Observe: ]]--

A = game.Workspace.BrickIWantToFind
A.Reflectance = A.Reflectance - 1
--[[
This script works as well. However, it cannot be entered into the command bar. This has to be in
a script object to work as it is 2 lines long.

----- Editing Multiple Values/Accessing Classes -----

What about those properties with multiple values such as position and velocity? The syntax to
access these values is simple. You put a pair of parentheses around the values (Which should be
separated with commas.) Example: ]]--

script.Parent.CFrame = ( 3, 45, 1 )
--[[
Note: Make sure you use the '(' parentheses, not the '{' parentheses.

This is how multiple values are accessed at once. This works a bit like an array or table
(which will be covered later). However, this is not a working command yet. These properties
have their own classes; and we must access the classes these properties are in. Let's take,
for example, the CFrame property. To CFrame a brick, we must access the class it's inside.
The CFrame property is inside the class Vector3. So, after taking a look at the syntax below,
can you figure out how to make a CFrame command?

(Brick).(Property) = (Brick).(Property) + (Class).new(values)

This command uses the "new" function to add the following values after it. Take a look at this
example CFrame command: ]]--

game.Workspace.CFrameBrick.CFrame = game.Workspace.CFrameBrick.CFrame + Vector3.new( 1, 1, 1 )
--[[
NOTE: This script does NOT relocate the brick to 1, 1, and 1. It moves the brick in that direction
by 1. Let's review the X, Y, and Z vertices before moving on to explain the script itself.

----- X, Y, and Z Coordinate Plane -----

If you have taken Algebra, you'll be able to understand this concept much better. In a 3D
environment such as Roblox, there are 3 directions, or vertices, that make up the entire world.
They are X, Y, and Z. X is width, Y is height, and Z is depth. 2D games only have the X and Y
coordinates. X is the first value, Y is the second value, and Z is the third value in any
property.

So back to the script above. Y is the value in the middle. Since that value is 1, the brick
moves up by 1. If it were -1, the brick would move down by 1.

----- Position -----

What if you just want to relocate the brick to a new position rather than moving it?

You access the Position property. The position property lies inside the Vector3 class too. Take
a look at this example statement that relocates a brick to position 100, 50, and 200: ]]--

game.Workspace.Part.Position = Vector3.new( 100, 50, 200 )
--[[
Rather than moving in a direction like CFraming, position sets the object at a certain place.
This is great for making things go away, but not moving bricks. You might have deduced that
instead, we can use the CFrame command to create a true moving brick later on. Just so you
know, Size is also in the Vector3 class and works the same way as these two. Here's an example
of a statement that changes the size of a brick: ]]--

script.Parent.Size = Vector3.new( 10, 1.2, 10 )
--[[
This sets the size of the brick according to the numbers you put in.

----- How about changing the color of a brick? -----

Changing the color of a brick is a bit complicated because each color is assigned a number. For
example, White is equal to 1. Navy Blue is 1011, for some reason. It won't be easy to remember
these numbers, but luckily, the Roblox Wiki has a color palette code right here:

http://wiki.roblox.com/index.php/BrickColor_Codes

Using this, you can know what color you will end up with. (If you don't enter a valid number, it
reverts back to Medium Stone Grey.) What's interesting is that there are a lot of hidden colors
too (which don't work).

The command for changing a brick color is simple enough. The syntax is:

Location of brick.Brick.BrickColor = BrickColor.new(value)

A bit like CFrame or Position, as it also uses the "new" function to change the values.
Here's an example command that sets a brick color to white: ]]--

script.Parent.BrickColor = BrickColor.new(1)
--[[
Using these commands, we can make a party dance floor!

----- What's the point of commands? -----

These commands actually make up most of our scripts. These commands are extremely important
to learn and get familiar with. For example, a door script might just simply be changing the
properties of the door so you can walk through it. An elevator, for example, is just using the
CFrame property. A rotating sign, a flashing dance floor, a brick that disappears when you walk
on it, these are all using these sort of commands. Next, let's cover the basic "while true do"
loop so we can repeat commands!

Now, it is time to take your next quiz! Locate "Quiz 5:Commands" in the quizzes script
and send your answers to me for grading via PM.

(Message me any questions you have on this more complicated subject!)

-xXAlphanumericXx
Report Abuse
LifeIsUnfairReturns is not online. LifeIsUnfairReturns
Joined: 10 Mar 2012
Total Posts: 4612
11 Jun 2013 12:04 PM
This is the last part


--------------------------------------------------------------------------------------------------
VIII - While True Do Loop
--------------------------------------------------------------------------------------------------

You've seen those disaster places on the front page. Or maybe those mini-game places that repeat
a certain function over and over. How do they work? They use something called a "loop". A loop
is simply a script that repeats something. There are several kinds of loops, but in this chapter,
we will cover the most basic: While true do.

----- What kind of loop is a "While True Do" loop? -----

A while true do loop is an unconditional loop. That is, it will execute no matter what. A while
true do loop goes on forever, not stopping unless there's an error. Loops like these require the
use of a function called "wait". Let's explore the wait function.

----- Wait! -----

The wait function tells the compiler to wait a certain amount of time before moving on to the
next line (Remember that the compiler reads every line in order from top to bottom.) The proper
way to perform a wait function is simple. Here it is:

wait(number of seconds)

The value you put in the parentheses after "wait" is how many seconds long the compiler waits.
Actually, I think the value is maybe a little less than a second each number, but it's very close
to a second. Here is an example that waits for about 10 seconds: ]]--

wait(10)
--[[
You MUST include at least 1 wait line in a While True Do loop or else it uses up extreme memory
and usually crashes.

----- How to write a While True Do loop -----

Just below is the syntax for a While True Do loop:

while true do

(your code here, should include 1 wait line to tell how often the code is repeated)

end

Above, while true do is used to start the loop. Add in whatever code you want to repeat next,
and then enter "end". Any time you perform a function, loop, if statement, you must add an
"end" to end the function. This goes for almost everything except comments, of course.

Now time for an example! This is a script you should practice using. Can you guess what this
loop will do? ]]--

while true do
print "This is a loop"
wait(5)
end
--[[
It prints "This is a loop" every 5 seconds in output. Seems simple enough, right? How about a
more complicated loop? You can use multiple waits and have a loop perform multiple functions
such as this one: ]]--

while true do
print "The brick turns invisible!"
game.Workspace.Brick.Transparency = 1
wait(3)
print "The brick appears again!"
game.Workspace.Brick.Transparency = 0
wait(3)
end
--[[
If there's a brick named "Brick" in Workspace, this script works! Remember not to
capitalize while, true, do, or end. They are keywords.
Again note that statements will make up a lot of our scripts.

As stated earlier, the while true do loop runs unconditionally. Do you want to make a conditional
script that performs a function if something else is true? Next, we'll cover If Statements!

Now, it is time to take your next quiz! Locate "Quiz 6:While True Do" in the quizzes script
and send your answers to me for grading via PM.

(Send me any of your questions via PM please!)

-xXAlphanumericXx

--------------------------------------------------------------------------------------------------
IX - If Statement
--------------------------------------------------------------------------------------------------

Think: What is a condition? It's the way something is. If something is conditional, it depends
on something to be true. This is a lot like how If Statements work. They check a condition,
and if it's true, they perform a block of code. However, if the statement isn't true, the
block of code is skipped. Let's take a look at how to write these statements.

----- If this is true, Then do this: -----

If Statements work a lot like the sentence above. If Statements also require an end! I forget
that a lot. If you don't put an end to your If Statement, it will error! Here's the syntax:

if (condition) then

(Your code here)

end

Don't capitalize "if" or "then". Now, you have to place a condition between if and then. Perhaps
now is a good time to review what are called conditional operators.

----- Conditional Operators -----

You're already familiar with one of these in a way: It's the equal sign. Recall that an operator
is a symbol between two values or variables that performs an operation. You know the mathematical
ones which perform math operations (e.g. +, -, *). Now take a look at the conditional
operators which are used to check a condition:

== - Equals sign, checks to see if two things are equal.
>= - Greater than, checks to see if the value on the left is greater.
=< - Less than, checks to see if the value on the left is lower.
=|= - Not equal to, checks to see if the two values are not equal to each other.

NOTE: When declaring variables, you use only 1 equal sign (=). But when using If statements to
check if two things are equal, use TWO (==) equal signs! This is another thing I always forget.
Therefore, you should remember it.

----- Example If Statement -----

Take a look at the if statement script below, which checks to see the greater of the two values.
What do you think it will print?
]]--
A = 10
B = 20
if A >= B then
print "A is greater than B"
end
--[[
This will print nothing because B is greater than A. What if we want something to print if B is
greater than A? One might try adding a print function after the if statement incase B is greater
than A, like so: ]]--

A = 10
B = 20
if A >= B then
print "A is greater than B"
end
print "B is greater than A"
--[[
But if A was greater than B, it would still print both statements. If you want to make it where
if something isn't true, then it performs another event, look into "else".

----- Else -----

Else is a keyword that basically says if a condition is not true, then the script should do
something else. There are two uses of else: Else, just on its' own, and then ElseIf, which asks
another condition. You can have only 1 else for each time you start an If Statement, but you can
have as many ElseIfs as you want. Here's the syntax for an If Statement script that uses "else":

if (Condition) then
--code
else
--code
end

So think back to the script above. If we put that second statement into the "else" block of code,
the whole script will work properly! Here's an example that checks a property of a brick, and uses
else just in case the If statement is false.. : ]]--

Brk1 = game.Workspace.Model.ShinyBrick
if Brk1.Reflectance == 1 then
print "The brick is shiny!"
else
print "The brick is boring."
end
--[[
The power of else goes much further than this. Look at this script that might work for a shop: ]]--

Cash = 500
if Cash >= 800 then
print "You have enough money to buy the Sword of Ossim!"
Cash = Cash - 800
else
print "You don't have enough money to buy the Sword of Ossim."
end

----- Else If -----
--[[
Now, if we want to make different things happen for multiple conditions, we use elseif. Syntax:

if (condition) then
-code
elseif (condition) then
-code
(you may add as many elseifs as you want)
end

This is how disaster places work, usually. They randomize a number and use elseifs to check the
many possibilities of what the number was. Then they perform the disaster inside the elseif block.
Look at this example script: ]]--

if A == 1 then
print "Possibility 1!"
elseif A == 2 then
print "Possibility 2!"
elseif A == 3 then
print "Possibility 3!"
end
--[[
Note that elseifs don't need their own ends. See how if, else, and elseif work?
If Statements are the main conditional scripts you'll be working with in your own places.
Now, what if you want to check multiple conditions at once, but just for one event?

Use "And".

----- This AND That -----

Perhaps you want something to happen only if TWO things are true. Or maybe even three. You can
do this using the "and" keyword. You can place the "And" keyword between conditions so that the
compiler knows all conditions must be true for the next block of code to execute, or happen.
Basic syntax is:

if (condition) and (condition) then
-you know the rest
end

You can use And with "Elseifs" the same way too. Here's an example script just in case: ]]--

if a == 1 and b == 1 then
print "A + B = 2"
elseif a == 2 and b == 2 then
print "A + B = 4"
end
--[[
Perhaps a more logical example, here's a script that checks if a value is within a certain range
(Much like inequalities in Algebra): ]]--

if value <= 100 and value >= 50 then
print "The value is between 100 and 50."
end
--[[
This is another script that is good to practice.
Up next, we start getting into the really good stuff: Functions!

Now, it is time to take your next quiz! Locate "Quiz 7:If Statements" in the quizzes script
and send your answers to me for grading via PM.

(IF you have any questions, THEN send them to me. ELSE, just keep reading.)

-xXAlphanumericXx

--------------------------------------------------------------------------------------------------
X - Basic Functions
--------------------------------------------------------------------------------------------------

We've covered basic scripts so far; but none of them are truly user-activated. Perhaps you want
to make a click button which opens a door, or a trigger block that makes a pile of trash fall
on the user if they step on it. Perhaps, most famously, a lava blocks that kills you upon touching
it. Well, basic functions can do all of this and more.

----- What is a function? How does it work? -----

A function is a triggered event. That is, it's a block of code that can be triggered by something
happening inside the game. Functions can be triggered by virtually anything. After creating a
function, we can either give it a trigger, or just run it using a simple line of code. You give
a function a name, and then call that name later on to run the code again. Although there are
plenty of triggers (and I encourage you to explore using them), here are the basic triggers which
we will cover this chapter:

*Touched --The function will execute if the script's parent is touched
*Clicked --The function will execute If the script's parent is clicked (by a mouse)
*PlayerEntered --The function is executed immediately when the player enters

----- How to declare a function? -----

There are several different ways to write a function, but the syntax for the way I prefer to write
functions has four main parts:

{
(The Declaration)

(The Event)

(The End)
}

(The Connection Line)

First, we'll cover declaring a function.

----- The Declaration -----

There are several different ways to declare a function, but every function starts with declaration
line. The most common (and I prefer this way too) uses this syntax:

function (function name goes here)()

Here is a simple example.
]]--
function FunctionName()
--[[
This is actually a working line. Here, the function name (which we need to remember so we can
call the function by its' name later) can be anything. It is completely variable (but do follow
the basic rules, such as the fact that it can't start with a number or be a keyword.)

----- The Event -----

This is the part of the function where you perform the action that is triggered. This is the
code that is triggered by a certain action. Let's say we want to print "Hello" in output after
the user touches a certain block. The user touching the block is the trigger and the "Hello" being
printed is the event. You put the event here. In this example, our event is simple: a print
statement. See the example below.

----- The End -----

Afterwards, when you've finished writing your block of code, you add an "end" on the end like so: ]]--

function Hello()
print "Hello!"
end

----- Running a Function -----
--[[
You can run any function using a simple command that goes like this, referring to its' name: ]]--

Hello()
--[[
This tells the compiler to run the function above. This would print "Hello" in output.
You can use this command to simply test the event block before you assign a trigger.
Let's explore assigning a trigger to a function so we can do something more powerful.

----- The Connection Line (Or Bind) -----

Your function needs to be connected to a trigger for it to be trigger-activated. This is
a complex statement, and it's easy to forget the order that the words come in. Another
thing you have to remember is that the connection line comes after the end, not before it.

----- The Syntax -----

The connection line for this kind of function always begins with "script.". After that,
you find an object to put a trigger into. Once you have found the object to put the trigger
into, you assign the proper trigger and then connect it to the name of your function.
Here is the basic syntax:

script.(whatever object you want to add a trigger to).(trigger):connect(functionname)

Looks complex. But it's a lot shorter than that. Let's say we want to add a "Touched"
function to the parent of our script. It's as simple as this:
]]--
script.Parent.Touched:connect(functionname)
--[[
Remember to put a colon between Touched and connect, not a period. Yes, the function
name is always in parentheses. It's not a string so don't add quotation marks.

Let's create a connection line for our script above that had a print function. All
we really have to do is change the function name from the example above. Here is our
finished function:
]]--
function Hello()
print "Hello!"
end

script.Parent.Touched:connect(Hello)
--[[
Now, if we put this script into a brick, and then touched the brick in play mode, it
would print "Hello" in output. Pretty impressive, right? Let's try another example.

Let's say you have a button block in front of a door block. What if we want to make it
where when we touch the button block, the door disappears, but for only five seconds.
It's another touched function, so the connection line is essentially the same. What
changes is our event. Here is the example:
]]--
function Button()
game.Workspace.DoorBrick.Transparency = 1
game.Workspace.DoorBrick.CanCollide = false --Turning the brick invisible and making it uncollidable
wait(5)
game.Workspace.DoorBrick.Transparency = 0
game.Workspace.DoorBrick.CanCollide = true --Turning the brick visible and making it collidable
end

script.Parent.Touched:connect(Button)
--[[
Once you scripts start getting this large, it's a good idea to start using comments
like the ones above. It keeps everything organized and you'll know exactly where you
are in your script.

Now you see the power of basic functions. It's a good idea to memorize these scripts
because you'll be using these sort of functions a lot in your places.

Remember from above that Touched is not the only trigger we can assign. Let's try
creating a Clicked function!

----- Clicked! -----

Creating a clicked function is different from creating a touched function in a few ways:

*The connection line changes.

*You must add a ClickDetector object.

Otherwise, the syntax remains the same.

Let's start with a simple function that causes a block to fall on a user.
Let's assume block A is above the click button. When the user clicks the button,
we are going to make it fall on their head by un-anchoring it. Here's the function
part.
]]--
function DeathTrap()
game.Workspace.BlockA.Anchored = false
end
--[[
Now that that's done, let's get to the syntax of a clicked function connection line.

----- Syntax of a Clicked Function Connection Line -----


I haven't finished it yet

I quit writing it for some reason
Report Abuse
Darthnoodles98 is not online. Darthnoodles98
Joined: 02 Apr 2011
Total Posts: 418
12 Jun 2013 10:50 AM
@Above, couldn't you make a script and put a link to it?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image