Absolute beginner's guide to scripting: Difference between revisions

>JulienDethurens
>JulienDethurens
Line 97: Line 97:
There are 5 topics that will be answered here:  
There are 5 topics that will be answered here:  


* Creating the Script
* Creating the script


* Storing references to Objects
* Storing references to objects


* The Listening Event
* Listening to events


* The Function
* Functions


* Modifying Objects
* Modifying objects


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".  
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". You can also press CTRL+N or click File>New to get a completely empty place.


=== Creating the Script ===
=== 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.  
To get a script, go in Roblox Studio and 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.
Now to open the script, just double-click it. If you did it right, a window will cover the whole ingame screen. And you will find the line "print "Hello World!"" Before you start, just go ahead and delete that line.


=== Making references to Objects ===
=== Making References to Objects ===


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.
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.
Line 121: Line 121:
Now, making references to objects is not necessary, but it can make scripting a lot less work. Here's an example of how you do it:  
Now, making references to objects is not necessary, but it can make scripting a lot less work. Here's an example of how you do it:  


<pre>local brick = game.Workspace.Brick</pre>
<code lua>local brick = Workspace.Brick</code>


That will make `brick` refer to <code>game.Workspace.Brick</code>. You can set the name to absolutely '''anything'''. This is an example of a [[variable]]. You can have as many variables as you want.  
That will make `brick` refer to {{`|game.Workspace.Brick}}. You can set the name to absolutely '''anything'''. This is an example of a [[variable]]. You can have as many variables as you want in a script.


If you didn't store it in a variable, every time you try making the script modify the object, you would have to put the line "game.Workspace.Brick" every single time. Variables make it much simpler, since you would only have to put the name you assigned. Name the brick desired to "Brick", and make a reference to it in the script by typing the example above. Note that making a reference to an object is the same as assigning a variable.
If you didn't store it in a variable, every time you try making the script modify the object, you would have to put the line "Workspace.Brick". Variables make it much simpler, since you only have to put the name you assigned. Name the brick desired to "Brick", and make a reference to it in the script by typing the example above. Note that making a reference to an object is the same as assigning a variable.


=== Event Listeners ===
=== Event Listeners ===


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.  
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.


An event listener watches what's going on, and waits for an event to happen. When it does, it tells the script to do something. This is one important part of the script, otherwise you couldn't really make scripts wait for anything.  
An event listener watches what's going on, and waits for an event to happen. When it does, it tells the script to do something. This is one important part of the script, otherwise you couldn't really make scripts wait for anything.  
Line 135: Line 135:
You still should have the script with the variable in it. We're going to make the script listen for being touched (the [[Touched]] event). Here's an example:  
You still should have the script with the variable in it. We're going to make the script listen for being touched (the [[Touched]] event). Here's an example:  


<pre>brick.Touched:connect(onTouch)</pre>
<code lua>brick.Touched:connect(onTouch)</code>


If the brick is Touched, it will execute the [[function]] named <code>onTouch</code>. Keep in mind that the name inside the parentheses is the name of the function.
When the brick is touched, it will execute the [[function]] named {{`|onTouch}}. 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. [[Class reference|Here]] is a very well-done reference page set up by MrDoomBringer.  
This is not the only listener type. There are many more to use, some of which require some familiarity with scripting. [[Class reference|Here]] is a very well-done reference page set up by MrDoomBringer.  
Line 143: Line 143:
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.  
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 <code>brick.Touched:connect(onTouch)</code> a line or two below the variabel assignment <code>local brick = game.Workspace.Brick</code>
Put the line <code lua>brick.Touched:connect(onTouch)</code> a line or two below the variable assignment <code lua>local brick = Workspace.Brick</code>


=== The Function ===
=== Functions ===


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.  
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.  
Line 151: Line 151:
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:  
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:  


<pre>function onTouch(part)
<code lua>
function onTouch(part)


end  
end  
</pre>
</code>
There is the function. As you can see, the word "function" is followed by "onTouch", the name of the function. 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 the "(part)" after the function name. This is a reference to the object that the listener found that touched the brick. This is not always needed, especially for some event listeners.
There is the function. As you can see, the word "function" is followed by "onTouch", the name of the function. 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 the "(part)" after the function name. This is a reference to the object that the listener found that touched the brick. This is not always needed, especially for some event listeners.


Line 166: Line 167:


This is where storing references to objects saves you time. We wanted it to flicker invisible/visible, so here's an example:  
This is where storing references to objects saves you time. We wanted it to flicker invisible/visible, so here's an example:  
<pre>
<code lua>
brick.Transparency = 1  
brick.Transparency = 1  
wait(1)  
wait(1)  
brick.Transparency = 0  
brick.Transparency = 0  
</pre>
</code>
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.  
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.  


Line 177: Line 178:
=== Complete script ===
=== Complete script ===


<pre>
<code lua>
local brick = game.Workspace.Brick --storing a reference to the brick
local brick = Workspace.Brick -- Store a reference to the brick.


function onTouch(part) --the function
function onTouch(part) -- The function that runs when the part is touched.
brick.Transparency = 1 --what the function is to do with the brick
brick.Transparency = 1
wait(1)  
wait(1)  
brick.Transparency = 0  
brick.Transparency = 0  
end  
end  


brick.Touched:connect(onTouch) --attach the event listener
brick.Touched:connect(onTouch) -- The line that connects the function to the event.
</pre>  
</code>
 


==Advanced scripting techniques==
==Advanced scripting techniques==
Anonymous user