RobloxHQ Forums

You are not logged in.

Announcement

Important Notice: Some of you may have borderline inappropriate avatars on purpose. I (Affinity) will ban those with said avatars if they toe the line on breaking a rule.
WARNING: THIS FORUM MAY BE UNSUITABLE FOR LITTLE KIDS. IF YOU THINK YOU'RE A LITTLE KID, PLEASE KEEP THAT IN MIND. Thank you.
DISCLAIMER: ROBLOX HQ IS NOT AFFILIATED WITH ROBLOX IN ANY WAY. NOTHING SAID HERE REFLECTS ON OR IS REPRESENTATIVE OF THE OFFICIAL ROBLOX WEBSITE AND COMPANY.
The Rule is just this - Don't be stupid. (More Rules)
  • Index
  •  » Roblox Stuff
  •  » Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

#1 2008-11-15 11:21 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Since this scripting guide is now comprehensive of what I feel are the basics, I'm opening this up for discussion.  Lesson #3 will have an extra scoop on tricks to use with if and while, as well as a few definitions of confusing scripting terms.  Lesson #4 will be the hardcore stuff -- stuff that I'm not even sure how to fully work yet.  =P

Bob10110’s Scripting Guide v1.27

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

If you’re looking for a bona fide way to improve your scripting skills, look no further than here!  In this guide I attempt to explain everything in simple terms (from your most basic script to the things I myself am currently learning).  Click on a link below in the Table of Contents to get to the respective section.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

TABLE OF CONTENTS
The Basics of Roblox Lua (geared towards complete newbies to scripting – mainly covers Roblox Studio and Properties)
The Scripting Concepts to Take it Further (covers the use of variables, Events, and if/for/while)
Tricks of the Scripting Trade (a few tricks one can use)
The Advanced Stuff I’m Attempting to Learn (a possibly inaccurate depiction of coroutines)

Last edited by bob10110 (2008-12-20 3:26 PM)


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#2 2008-11-15 11:21 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

The Basics of Roblox Lua
Simply put, scripts are a necessity in Roblox.  They allow you to do practically anything with your bricks, from doors only certain people can go through to buttons that regenerate something.  They can allow a brick to move, rotate, change colors, and quite a bit more.  Mainly, this lesson will teach you how to do stuff to an individual brick.

First, open up Roblox Studio.  To do this, look for it on the Start Menu.  If it's not there, click on All Programs, click on the "Roblox" folder, then on "Roblox Studio".

Open up a new Place in Edit Mode (I use the icon of the blank piece of paper in the top left).  Then click on View on the Menu bar and get all three of these things showing:
--Explorer (always helps to know what you're working with)
--Properties (the main part of this lesson)
--Output (a scripter's best friend)

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now insert a Brick, but don't do anything to it.  It's easy enough to do that from the Insert tab at the top of your game window, but now you probably ask yourself where the scripts are.  Click on the Brick you made, and click Insert --> Object on the Menu bar.  A huge list of things comes up for what you can insert into that Brick -- for now, just select "Script".

So now you have a Script -- you should be able to see it in the Explorer panel.  Double-click on that Script to open it up.  You should see this (unless something is terribly wrong):

Code:

print("Hello world!")

What this code does is create text in the Output panel.  This is extremely helpful for figuring out what your script is doing.  In this case, it just prints Hello world! and then runs out of script to do.  Let's start editing this script.

Create a second line with the code

Code:

print(1337)

And click on the Place1 tab at the top left corner of your game window.  Now click the Play button at the top (if you can't see it, click on the "Tools" tab in the game window).

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

You should get the output:
Hello world!
1337

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Once you're done, click the Purple-ish Reset button (next to the Play button you hit earlier) to put your place back to the way it was.

If you wish, you can try  print(random letters/numbers)  on your own.  However, you'll get either:
A) an error (if there was a number first) or
B) "nil" (if there was a letter first)

...when it goes to create Output text (unless you put quote marks around it).  This happens because it thinks your string of random letters is a variable.  I'll cover variables later on, in the second lesson.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now, let's get out of doing nothing and actually do SOMETHING to this brick.  Delete whatever coding you have inside the Script.

You've probably noticed that your brick is falling each time you click the Play button.  If not, then find it in your game window.  I won't cover how to move your view since you had the stupidity to move it off of the brick in order to not see it fall.  XD

Anyways, let's fix that with a Script.  Insert this code into your brick's Script:

Code:

script.Parent.Anchored = true

Roblox Lua has to interpret things separated by a period.  It's basically like your Menu bar, so inserting a new Script object would be interpreted as  MenuBar.Insert["Object..."].Script.  (That thing I made up there isn't real code, by the way, so don't use it.  Ever.)

The code in the above box sees "script", which is the Script object you're working with.  Then it sees "Parent", which is whatever holds this Script object.  In this case, the Brick you made.  Then it sees "Anchored", which is a Property of a Brick.  A Brick's Properties cover practically everything you'd want:  Position, Size, (Brick)Color, and much more.  If you want to see all of them, just look inside the Properties panel I had you open up earlier -- it should be in the lower right section of your screen.

I digress, though.  Once you put that code in and start up time again with the Play button your brick shouldn't fall (if you didn't click Reset earlier, you'll have to click that now, then put the  "script.Parent.Anchored = true"  code in again).

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now let's try this script.  Insert this code into your script and run it again with the Play button.

Code:

script.Parent.imafiringmahlaser = true

This is why you ALWAYS need your Output window open.  What this 3-line error message means is that your script is broken somewhere and thus has stopped running.  It will do stuff before the broken line, but not after it.  Let's analyze that error message:

Sun Nov 16 00:15:02 2008 - imafiringmahlaser is not a valid member of Part
Sun Nov 16 00:15:02 2008 - Workspace.Smooth Block Model.Script, line 1
Sun Nov 16 00:15:02 2008 - stack end

The first line is in red to tell you that your script is broken.  It complains about something that it can't understand (much like noobs these days).  In this error message, it tells you that "imafiringmahlaser" is not in your Brick -- either as a Property or as an object by that name.

There are various types of error messages, but most of the ones you'll see at first will deal with this example.

The second line says exactly where the script is wrong.  Workspace is the game world you play in when you're online, "Smooth Block Model" is the name of your Brick, "Script" is the Script object inside that Brick, and "line 1" is, well, the first line of the code (if you put that flawed code in the second line, it'll say "line 2" instead -- it's quite accurate).

The third line just says, "You fail, so I'm stopping your script."  =P

Just a side note before we get back:  The computer is very fickle about spelling.  It'll complain over the slightest misspelling, wrong punctuation, or even wrong capitalization.  I cannot stress enough how important it is to keep the Output window up to check for these errors.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Okay, now let's get back to creating code that works again.  Anchored can only have two values (true or false), but certain other Properties can have a large range of possible values.  Transparency is one such example (although anything higher than 1 or lower than 0 won't affect the brick any more than said endpoint values).

Erase anything inside of your script and put this in it:

Code:

script.Parent.Transparency = 1

When you run the place, your brick should immediately disappear.  It's still there, only now it's invisible.  Remember to reset the place -- then change Transparency to any value between 0 and 1.  Now run it again and the brick will become partially invisible depending on what value you entered.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now let's try something a bit more complex.  Let's redefine how big the brick is.  You know what to do by now:

Code:

script.Parent.Size = Vector3.new(2, 6, 4)

This script requires a different type of value -- a Vector3 value to be precise.  A Vector3 is really just three numbers grouped together -- and this is commonly used for anything dealing with Size or Position.

Run the place again, and you'll see that the brick gets taller.  This is because your brick initially had a Size of (2, 1.2, 4) but the script defined it as (2, 6, 4).

Try various combinations of numbers.  Include negative numbers if you like -- you'll notice that they just get set to the lowest positive value (which is in most cases 1).  And the size maxes out at 512 for any one number.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Once you're done messing around with the Size, try editing its Position.  It's the same exact thing, only the code will have "Position" instead of "Size".  Numbers also won't get changed like they did in Size.  If you can't see where the brick is, try putting its values closer to (but not exactly) this: Vector3.new(79, 0.6, 25) -- which is where the brick should be at.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

If you can get that working, you're well on your way to becoming a decent scripter.  Now I introduce a new piece of code:

wait()

What this very useful code does is "pause" the script, only beginning to run it again once the amount of seconds inside the parentheses has passed (or about 1/30 of a second otherwise).  Let's combine all that we've learned here into one script:

Code:

script.Parent.Anchored = true
wait(1)
script.Parent.Transparency = 0.5
wait(1)
script.Parent.Size = Vector3.new(6, 6, 6)
wait(1)
script.Parent.Position = Vector3.new(80, 5, 20)
wait(1)
script.Parent.Anchored = false

So, if you've been following along this whole time you should realize what this script will do.  Put this in and run it.  The brick should remain in place, turning halfway transparent 1 second later, then growing, then repositioning, then start falling to its doom.  XD

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

If you ever want to know what Properties you can change in an Object, the Roblox wiki is a great source.  Just press F1 while you're in Roblox Studio, and it'll bring up the wiki in your Internet browser.

Now you should be able to script a single brick to do just about anything.  I'll get into how to make a script affect bricks other than the one it's in later on in lesson #2.  Feel free to play around with the scripts a little.  Don't get discouraged if it's difficult -- it's always difficult regardless of how good you are.  Patience and persistence will go a long ways towards becoming a successful programmer.


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#3 2008-11-15 11:21 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

The Scripting Concepts to Take it Further
If you're here, you should know your way around Roblox Studio.  You should be capable of inserting bricks and scripts easily enough -- and know how to change a brick's Properties with a script.

All that scripts really do is modify Properties.  For instance, a SuperJump tool simply changes your Torso's Velocity property.  The trick, however, is in getting these Properties to change when you want them to.

This lesson mainly consists of Events, if/for/while statements, and variables.  Once this lesson is done you should be capable of doing most things in Roblox scripting (well...with enough patience, logical reasoning skills, and the Wiki).  Anyways, let's begin.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

First, let's start off by explaining variables.  It's rather simple, actually.

You can define any "word" that doesn't start with a number as a variable.  For example:

Variable
VARIABLE
v4r14873

Typically, you want to make it short & easy to type, yet explain what it's going to represent.  So v4r14873 is not going to be a common name for a variable.  It is, however, still legit so feel free to use it (if you really want to).

Variables are basically telling the computer, "Hey, copy whatever this variable is and paste it wherever you see that variable."

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Create a brick in Edit Mode, and put a Script inside of it.  Put this code inside the script:

Code:

variable = "You've got a new variable."
print(variable)

This script creates a variable to hold a string of letters.  Then it creates text in the Output of whatever that variable holds.

Feel free to change what the variable holds.  The variable can hold ANYTHING, so long as it is used correctly in the script below the point where it is defined.  A variable can even hold something that is calculated from another variable's value (or the exact value that other variable holds).  Here's an example:

Code:

varA = 4011
varB = varA / 3
print(varB)

After tinkering around a bit, you'll see if switching the sides of the equation will work.  If varA can equal 4011, why can't 4011 equal varA?

...I'll leave it up to you to decide that the computer is either an idiot or is particularly obsessive with having things run a certain way.  In either case, you have to have the variable that you're defining on the left.  Remember that it is like the way algebra is taught -- Y is always by itself on the left side of the equal sign.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now that you have a better understanding of variables (or so I hope), it's time to do something a bit more dramatic.  Let's get a variable to represent a huge chunk of code.  To do this, we need to use the word "function" before that variable.  We also need a set of parentheses after the variable (so that if we choose we can put in some values -- for now, let's leave that an empty set).  Lastly, we need the word "end" to tell the computer that it can stop copying the code into this variable.

Here's an example:

Code:

function test()
a = 3
b = 4
print(a+b)
print(a-b)
print(a*b)
print(a/b)
end

test()

This script will print four values.  But let's say we want to be able to set the numbers that "a" and "b" are each time.  That is quite easy to accomplish by using MOAR COWBELL (Just kidding.  I meant more variables).  Instead of defining A and B inside the script, let's have them defined as input.  Input is what we "put in" the function to make it work.  (Kinda like saying x=3 and z=1 to solve y=7x+z.)

Code:

function test(a, b)
print(a+b)
print(a-b)
print(a*b)
print(a/b)
end

x = 3
y = 4
test(x, y)

Now, it may seem confusing to you.  Why do we have A and B above whereas we have X and Y below?

Remember that variables are basically just taking a value and copy-pasting it wherever that value may be.  So test(x, y) becomes test(3, 4).  When the computer reads test(3, 4) it automatically puts the first input value (3) into the first variable (a) and does the same with all the others.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Just to make sure you're getting the right idea from this lesson, here's a slightly longer script:

Code:

function test(a, b)
print(a+b)
print(a*b)
print("----------")
end

x = 3
y = 4
test(x, y)

x = 2
test(x, y)

x = 0
y = 9000.01
test(x, y)

By now my "copy-paste" analogy may be starting to frustrate you a bit.  Just read it from the top, ignoring the function we created (at least for now).  First we define x and y.  Then we call the function, so the computer then reads through the function and does whatever is in that function.  When it's done, it returns back to the point we were at and resumes the normal script.  After a bit it reads our function again and goes back up to carry it out another time.  It gets done with the function, and goes back down to where we were.  This keeps on going until the end of the script.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now, I'll bet these very limited scripts are getting annoying (having set parameters on when they have to run).  I'll now tell you how to get the script to run when you want it to.  And now that you should know how to create a function, it's actually quite easy.

Roblox Lua has these Events that let you run a script when just about any event you could think of takes place.  Some examples include Touched (when the brick is Touched), Changed (when a value or property is Changed), and Died (when a human has Died).

For this next part, you'll need to create a second brick above the first one I hope you've got your script in so the Touched function can work.

Here is a relatively simple script.  The first line is there just in case you forget to anchor the first brick.

Code:

script.Parent.Anchored = true

function onTouched()
print("Hit!")
end

script.Parent.Touched:connect(onTouched)

To explain that last line:  The "script.Parent" part is the script's parent -- the brick.  The Touched part tells the computer to watch this brick and see if it hits anything.  When it does, the ":connect" part basically tells the computer to run the function by the name in parentheses.  Note how the onTouched does not have the parentheses at the end -- in the connection line it will never have those parentheses.  So how do I get input for that function, like what brick hit it?  Well, that is already arranged -- just look at how most onTouched functions are set up:

onTouched(hit)
onTouched(part)

In either case, it requires input and the Touched event is willing to oblige.  Of course, you don't have to let the input mean anything (by having an empty () when defining the function like I did above).  However, in most cases you want to have it mean something.

Now, try to edit the above onTouched() script into an onTouched(part) script that prints the part's Name property.  If you can do that, you're ready for this next part -- the part that can allow you to do just about ANYTHING in Roblox.

+ Show Spoiler [the Answer] +



▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

On to what may be the hardest obstacle to overcome -- using if, for, and while.  These make for some very potent combinations, as by allowing certain things to happen under certain conditions your range of possibilities will expand to a large amount of things.

First, the if statement.  Basically, you ask the computer if something is true, and if it is then it continues with the script.  Otherwise, it skips that part of the script.

Anytime you use the word "if" in a script you also need an "end" to let the computer know where to skip to if that statement was false.

Here's an example:

Code:

a = 1
if a > 0 then
print("Yes!")
end

Simple enough, right?  You ask the computer if "a" is greater than 0.  It's true, so it prints "Yes!".  Play around with the code, setting the variable a to any number you want.  Once you're done, let's try this:

Code:

a = 1
if a == 1 then
print("A is 1")
end

If you read that, you'll notice an extra equal sign.  Did I just mess up such an easy script?  Of course not, that's how it's supposed to be in if statements!  =P

Well, if you don't believe that last statement try removing an equal sign.  The script will show you an error in the Output (which you should have up -- if you've closed it, open it up and NEVER close it again...you can resize it however ^.^).

Now try this script.  It's nearly the same thing:

Code:

a = 1
if a ~= 1 then
print("A is not 1")
end

Basically, ~= means "is not equal to".  Mess around with these scripts as much as you want, for it's the last if-statements you're getting for awhile.

Anyways, I'd go further into if statements but I'll save that for Lesson #3 (hey, I need to have something to discuss in that lesson =P).

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now onwards to "for" and "while".  Just a quick warning whenever you use "while" -- if you don't put wait() in the proper location of a while loop it'll freeze up your Roblox window (forcing you to quit by using the Task Manager).  I'll discuss "for" first, as it's less dangerous to use.

Here is yet another script.  Put this into your current script:

Code:

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

Oh yeah, I forgot to mention...both "for" and "while" also need to have an "end" whenever they get used as well.

The script defines a variable "i" as 1, and tells it to go to 10 by 1's (you can change that increment with a third argument, but I practically never do that).  While the variable "i" has not reached the target value it will keep running.  If "i" is the target value, it runs one last time, and then gets out of the "for loop".

In this case, you see the numbers from 1 to 10 printed.  Feel free to change 1 or 10 -- it helps to experiment.  Just don't do anything too crazy, like printing all the numbers from 1 to 1,000,000.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

For is pretty easy to show you how to do.  But let's try putting a for loop inside of a for loop.

Code:

for i = 0, 9 do
     for j = 0, 9 do
          print(i, j)
     end
end

This will print the numbers side by side so you can see what is happening.  The first for loop tells it to carry out whatever is inside the loop from the time i == 0 to the time i == 9.  While i == 0, the for loop inside of that is taking j from 0 to 9.  After that inner loop finishes, the outer loop goes from 0 to 1.  The inner loop goes from 0 to 9 again, and this keeps repeating until i is 9.

You can nest loops like that to create some amazing results.  Any combination of if, for, and while can be used to create these.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Speaking of "while", I've been whiling away the time long enough.  Let's get to it.

WARNING:  THE GAME WINDOW WILL FREEZE IF YOU FORGET wait().

Now that the warning's out of the way, let's create our first code with a while loop.  It works exactly the same way as an if statement, but it will keep repeating as long as the statement is true.  Let's just say "while true" so we don't have to bother with any computations.  =)

Code:

t = 0

while true do
print(t)
wait(1)
t = t + 1
end

This is a simple enough script.  While it's running, it will keep a rough track of how much time has passed by printing a new number every second.

Edit the script however you like, but do NOT, under ANY circumstances, remove the wait() line.  Doing so will result in your window freezing and having to press CTRL + ALT + DEL to get Task Manager to close it.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Feel free to use this newfound knowledge in your scripting.  Whenever you come up with an idea, break it down into smaller and smaller chunks until you can satisfy each chunk with an if, for, or while statement.

That's it for Lesson #2!  Now go out there and script!


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#4 2008-11-15 11:22 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Tricks of the Scripting Trade
Well, assuming my first two lessons didn't discourage you (or if you already have some basic skills from the Wiki's tutorials) then this lesson is going to expand on your current knowledge.  After this lesson, you should be able to use the full potential of if, for, and while.  I'm also going to talk about a few things that might be useful in complicated problems.

To proceed effectively, one needs a skill level of 50 in the following stats:  Using IF-statements, Using FOR-loops, Using WHILE-loops, and of course having a SENSE OF HUMOR.  =P

Let's get started then.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

I'm sure by now you've used the if-statement several times.  Perhaps you happened onto some slightly more difficult problems, such as this one:

There are four Teams in a Roblox game.  They are Red (21), Blue (23), Yellow (24), and Green (37).  The numbers in parentheses refer to the BrickColor's Number property that represents the color.  Now let's say you wanted to do something specific for members of one team, such as give all of them a neat weapon that you happened to put somewhere people can't reach.  How would you do this?

The answer is obviously going to include the if-statement.  It should also involve a for-loop so that we can go through all the Players in a neat and orderly fashion.  But how exactly do we go through the Players?  To understand the answer to that, I'll temporarily switch focus from a useful script to one that is completely useless.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

This particularly useless piece of code creates a table and is supposed to print out a given value.  For now, we can just pretend that these numbers are our Players.  Create this script in an empty Place, and make sure THAT THE OUTPUT WINDOW IS OPEN (otherwise you'll miss what the script does).

Code:

--Tables have curved brackets.
myList = {123, 456, 789, 808, 1337, 10110}
print(myList)

After testing this out, you probably got something weird in your Output such as "table: 0EE0BB10".  Don't fret, as even I got a bit confused when trying that out for the first time myself.  It doesn't matter much, anyways -- who in the world would ever want to print the table itself?  All you'd want to print anyways are the elements of the table, and not the actual table object itself.

I'll skip the seven minutes of frustration you'll probably have by telling you to try this script below out.  The only change is in the print line.

Code:

myList = {123, 456, 789, 808, 1337, 10110}
print(myList[1])  --Added [1]

Now run it, and you should see the first term (123) being printed.  Mess around with that, and feel free to throw in all sorts of numbers into the brackets.  Try out 2, 3, 1.5, math.pi, whatever you want.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

So how exactly did that work?  The reply to that question is a simple one -- indexes.  Basically, everything in a Roblox game gets filed into a neat and orderly file folder.  That folder is essentially what the Explorer window is.  I'd liken it to an upside-down tree (the Roblox "DataModel" -- the game itself -- is at the very top whereas an individual Property would be the end of a branch at the bottom).  In Roblox Lua, they start at 1 and count upwards by 1's. (Real programmers start with 0 'cause that's how it's done in C...of course, Roblox Lua ~= C.  XD)

I can use indexes in a for loop to tell the computer to print all the values of a table quite easily.  Take this addition to the useless printing script, for instance:

Code:

myList = {123, 456, 789, 808, 1337, 10110}

for i = 1, 8 do
print(myList[i])  --Note that the index is [i], which will keep changing throughout the loop.
end  --Remember, the for statement needs an end to go with it.

Yes, I realize I overshot the mark.  Let's fix that up quickly with something that will never fail us.

Code:

myList = {123, 456, 789, 808, 1337, 10110}

for i = 1, #myList do  --"#" put before a reference to a table basically means to count the number of elements in the table.
print(myList[i])
end

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now after seeing the capabilities of indexes, we're ready to return to our original problem.  Here it is again if you needed a recap:

There are four Teams in a Roblox game.  They are Red (21), Blue (23), Yellow (24), and Green (37).  The numbers in parentheses refer to the BrickColor's Number property that represents the color.  Now let's say you wanted to do something specific for members of one team, such as give all of them a neat weapon that you happened to put somewhere that nobody can reach.  How would you do this?

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

First of all, let's assume that the weapon is in Lighting.  That's where weapons that you want to award certain people typically go (since they'll disappear if you pick them up in the Workspace).  Go and insert into the Lighting any Hopperbin you'd like, whether it's from the Free Models list or a new, blank one from the (Insert --> Object...) menu at the top.

Next, we need to insert a script that will give that weapon in Lighting to those that meet the condition of being on a certain team (say, the Green team).  We'll need to figure out on what basis we want to give this weapon out.  We can either:

A)  Give the weapon out to people on a certain team that touch a certain brick
B)  Give the weapon to people on a certain team as soon as they spawn (slightly more difficult)

Now, I'm sure you could use a couple if-statements to determine that it is indeed a Player that's touching a brick, and then one more to check the Player's "TeamColor" Property (which is exactly like BrickColor).  So I'm going to take the harder road and teach you how to find out when a Player is respawning.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

To find out when a Player is respawning, we need to look at their Character.  Think of the Character as a "Property" of the Player.  Now here's the tricky part...how do we know if it was the Character Property?

Whenever you connect something through an Event, it typically gives the computer some data.  For the Touched Event, it returns (gives the info to the computer) the Part that touched it.  In a Chatted Event, it returns the message sent, the recipient (which is either everybody or teamspeak), and the person who sent that message.  You need not use that information in your scripting, it's just there if you do need it.

So for what we want, we'll want to know when the Character Property has been Changed.  This can be accomplished through this code (it only works if you test it in Online, as Solo brings up your Character before this script starts running):

Code:

function onPlayerEntered(newPlayer)

while (newPlayer ~= nil) do
change = newPlayer.Changed:wait()
print(change)
if change == "Character" then
print("Respawning " .. newPlayer.Name)
end
end

end

game.Players.ChildAdded:connect(onPlayerEntered)

The basic logic behind this is that when a Player comes into a game, the script runs for that Player.  The ChildAdded Event can't tell you when that new object changes, however, so we need the Changed Event.  To get the Changed Event, we create a new connection.  There's actually two ways to do so, but using this way will let us keep the "newPlayer" variable so we can reference it later on in this script.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

So now that we know that the Player's respawned, we can check their TeamColor and give them a weapon if they meet that requirement.  The statements follow as such:

Code:

wait(0.5)
if newPlayer ~= nil then  --Have to make sure the Player still exists, otherwise it could break the scripting.
if newPlayer.TeamColor == BrickColor.new(37)  --Green team ftw!
local weapon = game.Lighting.WEAPON_NAME_HERE:clone()  --Change that to match the name of the weapon in Lighting.
weapon.Parent = newPlayer.Backpack
end
end

First we wait a bit, because the Character might not be there to give the weapons to.  It's done much more noticeably in Kiseki CTF, but I feel that waiting half a second is just as good.  Then we check to make sure the Player hasn't left.  If the Player left, the script would break had we not checked for that caveat.

Then we check the Player's TeamColor.  In this case, I prefer to give the green team stuff.  Just a note here:  I'm not entirely sure if one has to use TeamColor.new or if I'm alright with BrickColor.new.  They're very similar objects that are based off of the same Color Reference.

After making sure we have a person on the correct Team, we then proceed to make a copy of the weapon in Lighting (we can't give it to them directly, because then that weapon would disappear) and give it to them.

Then we have to close up the if-statements with their respective ends.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

So, the whole script that gives a weapon to Green Team members on respawn will look like this:

Code:

--Automatic weapon-giving to (Bright) Green Team script:

function onPlayerEntered(newPlayer)

    while (newPlayer ~= nil) do
        change = newPlayer.Changed:wait()
        if change == "Character" then

            wait(0.5)
            if newPlayer ~= nil then  --Have to make sure the Player still exists, otherwise it could break the scripting.
                if newPlayer.TeamColor == BrickColor.new(37)  --Green team ftw!
                    local weapon = game.Lighting.WEAPON_NAME_HERE:clone()  --Change that to match the name of the weapon in Lighting.
                    weapon.Parent = newPlayer.Backpack
                end
            end

        end
    end

end

game.Players.ChildAdded:connect(onPlayerEntered)

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

Now, that's great and all but I have that sneaking suspicion you don't trust anybody who waltzes in and claims to be on the Green Team.  Perhaps you'd like to find out if they have a Hat -- that is what all true warriors strive for!

I shall now show you how to go about doing this.  Keep in mind we're not searching for a specific Hat, we just want to find out if they have a Hat.  This will show you several new concepts at once, which I'll explain in comments.

Code:

local hasHat = false

local char = newPlayer.Character
if char ~= nil then
    local c = char:getChildren()
--[[This creates a table of every object that is
inside what ":getChildren()" is called on.
In this case, it's the player's Character.]]
    for i = 1, #c do
        if c[i].className == "Hat" then  --Remember the beginning of this lesson?  "[i]" is an index.
            hasHat = true
            break
--[[If there is a for-loop or while-loop you
ever want to end while it's still looping through,
use "break".  It will end the looping at the end of
its current pass.

In this case, I don't see any need to look
for another Hat when I've already found 1.]]
        end
    end
    if hasHat == true and newPlayer.TeamColor == BrickColor.new(37) then
--[[The simple if-statement can evaluate more than one statement at once.
If you want both statements to be true, use "and".
If you just want at least one to be true, use "or".]]
        local weapon = game.Lighting.WEAPON_NAME_HERE:clone()  --Change that to match the name of the weapon in Lighting.
        weapon.Parent = newPlayer.Backpack
    end
end

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

And just to be fair, let's give each of the other teams some (lesser =P) weapon.  However, I don't feel like making an if-statement containing two and's as the more code one puts in, the higher the chances of your messing something up.  Let's just make an if-statement that is true if the player's team is anything but Green.  There's two ways to do this, both rather simple:

Code:

if not (newPlayer.TeamColor == BrickColor.new(37)) then
if newPlayer.TeamColor ~= BrickColor.new(37) then

I'll leave it up to you to figure out how to use that one.  Anyways, now you know most of what I use -- with the exception of what is to be had in my next post.  You might never need anything that I'll be showing you there, but each of them will have its uses.  If you've understood everything up to this point and are able to apply it, congratulations!  It took me somewhere around 2 whole months to reach a full understanding of if-statements (through playing around with others' code in Edit Mode and Script Builders), and assuming you haven't been nonchalantly glancing at this every couple weeks you've done it faster than I have (of course, times have changed... =P).

Coming soon:  Moderately Advanced Roblox Lua (to be honest, I'm realizing that I'm nowhere near "Advanced" status -- probably "Intermediate" at best).

Last edited by bob10110 (2008-12-20 3:18 PM)


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#5 2008-11-15 11:22 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

(reserved)


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#6 2008-11-19 2:39 PM

thenewmaniac
Forum Veteran
From: teh 1337 pwnage land
Registered: 2008-09-13
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Huh, That was helpful, thanks.
Also whats with the

bob10110 wrote:

(reserved)

?

Last edited by thenewmaniac (2008-11-19 2:40 PM)


The cake is a PIE!!!  How on earth was that 17% non-noob?
My Roblox account name is: swmaniac
I have now reached 150 posts, only 200 more until VIP at the meeting place. :)
Why is it that EVERY thread I post on seems suddenly die?

Offline

 

#7 2008-11-19 3:05 PM

RagnarokZaqw
Forum Super VIP
From: C:\Program Files\Games
Registered: 2008-09-26
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

thenewmaniac wrote:

Huh, That was helpful, thanks.
Also whats with the

bob10110 wrote:

(reserved)

?

reserved is for add more lessons


Steam: Giygas(or anything related to Mother game series), for most accounts I am: xGameMasta or Godrik1445.

Offline

 

#8 2008-11-19 5:18 PM

Kretin
Forum Super VIP
From: The infinate timeline
Registered: 2008-10-11

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Can you do player entered stuff next?


In thy Deathbed I die. In thy Deathbed I wake. My presence is eternal.~Eternalseph
In the Deathbed I lie dreaming of my eternal though~Everlasting
I have awaken my presence now eternal, everlasting, eternal, Forever~Kretin
http://forums.robloxhq.com/viewtopic.ph … 5&p=48  Click to ban me and others.

Offline

 

#9 2008-11-19 5:30 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

I'd say Player-entered scripts fall into the second lesson.  You use the "ChildAdded" event to watch game.Players:

Code:

function onEntered(Player)
if Player.Name == "Player" then
print("Player has entered.")
end
end

game.Players.ChildAdded:connect(onEntered)

Remember, if you want to know about something the Wiki is a great source.  Press F1 while you're in Roblox Studio to open it up.

I plan on adding Lessons #3 and #4 later on.  3 will show you some tricks you can use with if/for/while statements, and 4 will be pretty much the stuff almost nobody ever touches (stuff like coroutines, which I am still trying to figure out).


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#10 2008-11-19 5:45 PM

GreyUltimatum
Banned
From: now blood and steel
Registered: 2008-06-19
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

A scripting tutorial I actually understand...

http://i537.photobucket.com/albums/ff332/GrayUltimatum/wtfh4x.jpg


gone are the days
when freedom shone
now blood
and steel meet bone

Offline

 

#12 2008-11-19 6:01 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Hmm...well, I hadn't quite thought of where I'd explain all the various Roblox objects.  Might as well be here in this post.

You know how in the first lesson I told you how to insert a Script object by clicking Insert --> Object... on the Menu Bar?  Just do that same thing for inserting a Hopperbin object.  Then put a Script object inside of it.

You'll need two Events for a Hopperbin:  "Selected" to watch when the Player selects the Hopperbin, and "Button1Down" to watch for when the Player clicks on the screen.  I'll provide the link to that page below, so you can get a better idea of how that works (I'll post a sample code later on, after I eat).

Roblox Wiki Tutorials
Hopperbin Tutorial


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#13 2008-11-20 12:45 AM

OzJB1
Forum Veteran
From: SPARTAAAAAAAA!f Posts:∞
Registered: 2008-09-10

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

This is one of the best tutorials I've seen. I'm favoriting this. ;)


My ROBLOX: OzJB

Offline

 

#14 2008-11-20 5:17 PM

Kretin
Forum Super VIP
From: The infinate timeline
Registered: 2008-10-11

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

I guess I check the wilki for this too but how do I give on team a weapon and the other team another like wiht Conix and his classes
or a few other games


In thy Deathbed I die. In thy Deathbed I wake. My presence is eternal.~Eternalseph
In the Deathbed I lie dreaming of my eternal though~Everlasting
I have awaken my presence now eternal, everlasting, eternal, Forever~Kretin
http://forums.robloxhq.com/viewtopic.ph … 5&p=48  Click to ban me and others.

Offline

 

#15 2008-11-20 5:38 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Well, that's moderately difficult.  You have to have a script that watches each Player's Character property -- when that changes, it means they've just respawned.

When they respawn, use an if-statement to check their TeamColor (or in Kiseki, what class you are at the moment).  Give out different sets of weapons from game.Lighting (or wherever you put them in Edit Mode) to accomodate for each team/class.

Basically, I suggest going with a Player-entered script.  Then, inside the function that runs from the Player-entered Event, put another connection line to run a Character Changed script.

...yeah, that is moderately difficult.  I'd have a few problems with it myself, and I'm not even sure if the Changed event is completely fixed.  But the logic should be easy enough to understand.


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#16 2008-11-20 5:48 PM

Turtwig98
Forum Super Veteran
From: YOUR MOTHER
Registered: 2008-11-08
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Full of win maybe this can actually help me want to script! :D


http://i41.tinypic.com/2mdqvyw.png
Thanks ApplePiePod for the Turtwig avatar :)

Offline

 

#17 2008-11-20 6:25 PM

Kretin
Forum Super VIP
From: The infinate timeline
Registered: 2008-10-11

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Ok just wondering

so basically say soemthing liek this
player enter
check team
if team ( blah) then (idk what goes here how do i give them the weapon which i guess is in the lighitng?)
If team is (blah) then give them (blah)


In thy Deathbed I die. In thy Deathbed I wake. My presence is eternal.~Eternalseph
In the Deathbed I lie dreaming of my eternal though~Everlasting
I have awaken my presence now eternal, everlasting, eternal, Forever~Kretin
http://forums.robloxhq.com/viewtopic.ph … 5&p=48  Click to ban me and others.

Offline

 

#18 2008-11-26 1:40 PM

snateraar
Forum Super VIP
From: FRENCH TOAST
Registered: 2008-06-25
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

OzJB1 wrote:

This is one of the best tutorials I've seen. I'm favoriting this. ;)

Nvm,now i know the basics!
Thanks,Uncle Bob O_O

Last edited by snateraar (2008-11-26 1:56 PM)


http://i49.tinypic.com/2mqq34y.jpg
You mentioned the game therefore you thought of it therefore you lost it therefore you think 'Argh, I lost the game' and therefore you think of it again, resulting in an infinite loop.

Offline

 

#19 2008-11-26 1:42 PM

Byndley
Forum Super-Cool VIP
From: math.huge +1
Registered: 2008-07-08
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

What about Click Detectors?


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=Byndley http://i177.photobucket.com/albums/w204/UNX789/byndley2.gif http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=Byndley
Follow me on Twitter!
Thanks BlooWolf for the Signature! I'm hoping to get a new one, *hint* *hint*.

Offline

 

#20 2008-11-26 2:13 PM

Milez
Forum Fanatic
From: Right
Registered: 2008-06-24
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

I think I know how the classes work.

If shirtid = --the class shirt goes here

Offline

 

#21 2008-11-26 4:12 PM

DroneofWar
Forum Super Veteran
From: AAAAAAAAAAAAAAAAAAh
Registered: 2008-08-16
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Byndley wrote:

What about Click Detectors?

It's kinda the same as OnTouched.
i.e.

Code:

function OnTouched()
script.Parent.Parent.Blah:Remove()
end

Code:

function OnClicked()
script.Parent.Parent.Blah:Remove()
end

Offline

 

#22 2008-11-26 5:59 PM

bob10110
Doing everything in moderation.
Registered: 2008-07-03
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Yeah, pretty much the same thing...but there's one critical difference:

MouseDetectors can't tell who clicked the object.  It can guess based on each Player's position, but it cannot tell who clicked the object.

Connection line also looks slightly different:

Code:

script.Parent.ClickDetector.MouseClick:connect(---)

Assuming, of course, that the Script and the ClickDetector are both in the same object.  And "---" can be named anything, so long as you create a function above that used that name.  I might add the third section soon...but wait until I get back from break.


http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&UserName=bob10110
Scripting Tutorial
↑ ↑ ↓ ↓ ← → ← → B A
RHQ's current Bob-sponsored project:  Knytt Stories collab

Offline

 

#23 2008-11-26 6:25 PM

Flicky
Forum Super VIP
From: One 'er dem toxic waste dumps
Registered: 2008-06-20
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

*gets a case of the AHS (Asploding Head Syndrome)* I CAN'T KEEP ALL THAT IN MY HEAD. AAAAAAAAAAAAAAAAGH!!! *dies*


World of Warcraft server: Moon Guard (RP)
World of Warcraft MG name (Horde, Tauren): Whitepelt
http://pajamaforest.com/images/PajamaForestBanner.png

Offline

 

#24 2008-11-26 9:43 PM

RockLee96
Forum User
Registered: 2008-11-26

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

You didn't really mention this and I've tried so much!
But what about this script, it turns the brick anchored, makes it's transparency to 0.2, then makes it a ball

script.Parent.Anchored = true
wait(1)
script.Parent.Transparency = 0.3
wait(1)
script.Parent.Shape = Ball --THIS IS THE PART THAT NEVER WORKS!!

Offline

 

#25 2008-11-26 9:46 PM

fireflywater
Forum Super VIP
Registered: 2008-08-04
Website

Re: Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

print(1*10^-26)
wait(3)
print(1/0)
wait(3)
print(string.reverse(1+(3*10)+300+((35*100)*2)))
wait(3)
print(string.lower("TEH ANTI YELLING SAGA!!!"))
wait(3)
print(string.upper("I'm as calm as patchy"))

Last edited by fireflywater (2008-11-26 9:49 PM)


http://i27.tinypic.com/5zmg7a.png
"Time is value."; "Explaining the joke ruins the joke."; "GameMaster761: they would you name the names of bugs"; "Kick XXXXX. reason: stop sucking" - TF2 admin abuser.
+ Show Spoiler  +

Offline

 
  • Index
  •  » Roblox Stuff
  •  » Bob10110's Scripting Guide. (Lesson #3 nearing completion!)

Board footer

Powered by PunBB 1.2.20
© Copyright 2002–2008 PunBB