Just lets make it long xD
---------- Post added at 11:42 AM ---------- Previous post was at 11:41 AM ----------
Hio, I like waffles. Hio, I like pancakes. Hio, I want 2 trillion points. Hio, Hio Hornstar.
---------- Post added at 11:45 AM ---------- Previous post was at 11:42 AM ----------
Adding A Script
Remember to use ROBLOX Studio, NOT "build", or "solo" to edit your scripts, because those modes will run your place when you enter, including running all of your scripts.
The script will execute as soon as your place loads, so none of the killing scripts from last tutorial are going to work in your place unless you are there.
IMPORTANT: When a script encounters an error, it stops running, or breaks. If your scripts ran into any errors the errors will display in the output bar. To access this bar, click View-->OutPut.
Editing a script in ROBLOX Studio
Now, your new script should open up a text editor that says
print("Hello World!")"What does 'print()' do?". Print() simply prints the contents, in the parenthesis, into the Output Window. Click the Play button in toolbar and look in the Output Window. Remember that the parenthesis means that it is a function. print(), is trying to function the content in the parenthesis. Some functions we can not call using the method operation. The method operation is ). We can not print a value of something using the method operation, but we can print using the function operation. print()
Testing your script
To test, open the Test solo mode as described before. This time, don't type anything in the Command bar. They should run automatically. Scripts run automatically and run very fast. They run so fast that they would have already have run in your place before you can even look in the output.
There are 5 topics that will be answered here:
- Creating the Script
- Tagging Objects
- The Listening Event
- The Function
NOTE: All work, building and scripting, should be done in Edit Mode. All these lessons will be shown as if you are in Edit Mode. To open Edit Mode, on your desktop screen, hit Start>Programs>ROBLOX>ROBLOX Studio. Then go to your profile, then click "Edit".
- Modifying Objects/Tags
Creating the Script
To get a script go in Roblox Studio or Solo Mode then simply select Insert>Object. Now a window will appear. In the newly appeared window, click on "Script" and OK. You should find the script inside "Workspace" in the explorer tab. If you don't see any explorer window up, go to View >Explorer.
Now to open the script, just double-click it. If you did it right, a window will cover the whole ingame screen, and the browser should look a bit more like Microsoft Word. And you will find the line "print("Hello World!")" Before you start, just go ahead and delete that line.
Assuming the script is still under Workspace, that is where your script will run. Let's say you want a brick turning invisible/visible, back and forth when touched. The script needs to know where that brick is before modifying it.
Now, tagging objects are not necessary, but it can make scripting a lot less work. Here's an example of tagging objects:
brick = game.Workspace.BrickThat will tag the brick under the name you assigned. You can set the name to absolutely anything. You can have as many tags as you want.
If you didn't tag it, every time you try making the script modify the object, you would have to put the line "game.Workspace.Brick" every single time. Tags are much simpler, since you would only have to put the name you assigned. Name the brick desired to "Brick", and tag it in the script by typing the example above. Note that tagging is the same as assigning a variable.
The Listening Event
Now we're getting into the meat of scripting. Sure, the script knows where the brick is, but that's all. It can't do anything else. Now we're jumping into a listening event.
A listening event is the trigger of the script. This is going to tell the script to do something if the listener finds the trigger fired. This is one important part of the script, otherwise you couldn't really make scripts wait for anything.
You still should have the script with the tag in it. We're going to make the script listen for being touched. Here's an example:
brick.Touched:connect(onTouch)If the brick is Touched, it will connect the (function). Keep in mind that the name inside the parentheses is the name of the function.
This is not the only listener type. There are many more to use, some of which require some familiarity with scripting. Here is a very well-done reference page set up by MrDoomBringer.
This is where you can find more help in the future, when you begin to understand scripting more. Not only does it show Events, but also shows other scripting references need for other aspects of scripting.
Put the line "brick.Touched:connect(onTouch)" a line or two below the tag "brick = game.Workspace.Brick"
Your script is getting better and better, but where is the function? Your script will break if it doesn't have one of those for the listener to refer the script to.
What is a function? It is where all your modifying work will be done. It is also an important part to your scripting. Without it, you could not make the script modify objects from listeners. Another example:
function onTouch(part)end There is the function. As you can see, the function has "onTouch". The listener from last lesson is trying to refer to the function. The listener is going to tell the script to run through this function and do whatever is found inside. Notice after "onTouch". This is the tag of the object that the listener found that touched the brick. This is not always needed, especially for different listeners. Most times, with other listening types unlike touching listeners, you would just place this:
function onTouch() end But back to what we're looking at. The tag is the object that touched the brick. You can play with this object for fun later. Now notice also two lines below the function: "end". You will need one of these for every function and other aspect of scripting, such as "if" statements. Always remember this when scripting.
Now, make the lines from first example, except put it two lines under the tag, and one line above the listener.
The script knows the brick, will wait until it's touched, and has the function to use. But it doesn't know what to do to the brick.
This is where tagging objects saves you time. We wanted it to flicker invisible/visible, so here's an example:
brick.Transparency = 1 wait(1) brick.Transparency = 0 These lines will alter the brick as we wanted. The brick's transparency is changed to "1", which is completely invisible. The "wait(1)" line will make the script wait for one second before continuing, then the brick's transparency will be put back at 0, which is completely visible. You can alter "wait(1)" to any number inside the parentheses. Whatever number you put inside the parentheses will be the amount of time it will wait in seconds.
Put those 3 lines right under the line "function onTouch(part)" and above the line "end".
brick = game.Workspace.Brick --tagging the brickfunction onTouch(part) --the function brick.Transparency = 1 --what the function is to do with the brick wait(1) brick.Transparency = 0 end brick.Touched:connect(onTouch) --listening event
Advanced scripting techniques
Make your own functions
While scripting, you may want to make your own function to call later. This is easy enough, here is the syntax (grammar) for doing so: (NOTE: These functions are called without a colon [:])
function <functionname>(<parameter>)<statements>end<functio nname>(<parameters>)A parameter is a way to give data to a function. An example of a parameter: a Humanoid has a takeDamage() function. You have to tell it how much damage to take. You would type Humanoid:takeDamage(100) to take 100 damage. (NOTE: the takeDamage function will not take damage if the humanoid has a For***ield. Use this function for weapons instead of directly setting the health, to prevent spawnkillers)
Functions can also return a value, which means they can be used instead of a constant (like a number) or a variable (covered below):
function <functionname>(<parameters>)<statements>return <valueobtainedfromstatements>end<somevariable> = <functionname>(<args>)Copy and paste these codes into a script for a better example:
function sayHello(name)print("Hello, " .. name .. "!")endsayHello("Bob")Example 2
function addNumbers(a,b)ans = a + breturn ansendanswer = addNumbers(1,2)print(answer) --> Prints in the output 3
Functions that return values Note the return statement in Example 2. The return statement automatically ends the function at that line, and then gives the value to the variable on the LEFT SIDE of the equals sign. Let's look at that again, this time with detailed comments:
--Define a function called addNumbers with the arguments "a" and "b"function addNumbers(a,b)--make a variable called "ans", and set it to the sum of a and b.ans = a + b--Return the variable called ans. This ends the function.return ansend--Set a variable called "answer" to the return value (ans) of addNumbers, with the arguments 1 and 2.answer = addNumbers(1,2)print(answer) -- Prints in the output 3Flow control
Flow control, or Control statments basically means doing different things depending on the situation. They can also control the way the code is executed in the end. There are two main ways to do it in Lua, both involve conditions.
A condition is a situation. A simple condition is this:
1 == 2(Note the "==". Always use that, and not "=".) Of course 1 isn't 2! So that would be false.
1 < 2That would be true, because 1 is less than 2.
These are all of the conditions that you can use:
== Is equal to< Less than> Greater than<= Less than, or equal to>= Greater than or equal to~= Not equal toA reminder about these conditions is you put the symbol first, and then the "=", although this doesn't apply to the less than, or greater than conditions.
The if statement does something only if a condition is true. Syntax:
if (<condition>) then<statements>endExample:
if (2 + 2 == 4) thenprint("All is right in the world")endThat would always print "All is right in the world", because 2 + 2 is always equal to 4.
There is also an else statement, which executes if the condition is false:
if (<condition>) then<statements>else<statements>endExample:
if (2 + 2 == 4) thenprint("All is right in the world")elseprint("Warning! Warning! Computer Self-Destruction!")endWhile
while <condition> do<statements>endThis does something over and over until the condition is false, or the break command is executed. People typically don't use this unless they want a never-ending loop, in which case their condition is true, and the loop will not terminate. Here is something very important: If this is an infinite loop, you MUST put a wait() function in your loop! Otherwise your computer/server will take every ounce of it's processing power to execute the code, because right when it's done, it wants to execute again. This WILL crash the server. This will make it wait so it has time to execute other things, and not crash.
while true doprint("Lagging up your computer...")wait(0.5)end