User:SoulStealer9875/How to Script
Ever wanted to learn how to script but don't know where to begin? This guide is for you. We will begin small, and gradually progress to higher levels of scripting. If you already have a basic understanding of scripting, you can skip it.
This is still being written, please understand that if something doesn't make sense, it doesn't mean it is going to still not make any sense in the stable version when this is finished.
If you have any ideas for this, you may write on the discussion page or send me a PM.
Introduction
In this first page we will learn the basics, such as Print and variables.
Setting up
First, create a script. Make sure you are in Roblox Studio, if you haven't already done so, or if it is not yet open, go to View -> Explorer and then View -> Properties.
We will be working with the Explorer and Properties panes now.
We will also be working with the Output pane, go to View -> Output to open it.
Go to Insert -> Object -> Script, in the Explorer pane you will notice that under Workspace is a new object, "Script". Double click on this to go into the script editor.
The print method is commonly used for debugging broken code that doesn't receive any output. We will learn about output later on. First create a script if you haven't already done so.
Double click on the script in the Explorer pane to go into the script editor. The first thing you will notice is that it says print 'Hello World!'. This is the default script when pulling one out of the Objects menu like we did earlier to create this one.
Close the script editor by clicking the little "x" to the left of the Explorer pane. Now to test this script, press the green play button located in Roblox Studio. I will not give you direct locations to items in Roblox Studio, as you should be familiar with this before starting the road to scripting. If you don't know where everything is in Studio then I suggest you don't learn scripting until you learn about the Studio Environment.
Did you notice that? When you pressed the play button, the output talked to us! The output said Hello World!. Why is this you wander? Because the default code in the script we created said print 'Hello World!' That's right! Anything between the quotes is printed in the output window.
Okay, now click the pink reset button by the play button in Studio and open up the script again. Let's have a play around. Try changing the text between the quotes and clicking play to see what it says now.
When you memorize the script, and when you're done playing. You'll be happy to know there are different ways of doing this. Like these:
print('Hello World!')
print("Hello World!")
print[[Hello World!]]
print "Hello World!"
Interesting right?
Also it doesn't have to be just one, you can print more than one thing using this method, check this out:
print("Hello", "World", "!")
Prints exactly the same, Hello World !.
Keep messing around with it, and see what you discover.
Variables
Variables come in different parts: Integer, Strings, Boolean, Instance. They are used to store data and can be accessed at any time using the same script. You cannot access a variable from a different script to the one it is stored on. You will have to use a global variable for that, we will learn of this later on.
Integers
We will first learn of Integers, these are numbers that have no fractional component. First we create a name for our variable, it can be anything you want. You cannot use spaces or special characters, you can use an underscore ( _ ) as a substitution for a space. I would recommend naming your variables descriptively, so you know what they are for and why you made them in the first place.
When you have thought of a name, type the name down and slap an equals sign after it. Here's an example:
Friends =
Now, as we're doing about Integer Variables at the moment, we will slap a number down. Let's pretend I have 3 friends, we will write the number 3 after the equals sign.
Friends = 3
There we go, we've created a variable called Friends and its value is 3. Now we can access it. There are millions of different ways to access a variable and conveniently we've learned the Print method. So we can print the variable's value, 3, in output, by writing the name of the variable.
Remember: Variables are case-sensitive!
print(Friends)
Now click play, notice how the output now reads the value you input into the variable. In my case it printed the number 3. Click reset now.
Notice how we didn't put any quotation or speech marks before and after the variable's name. If we did, this would be called a String and it would output Friends and not 3.
Strings
Next we shall learn about strings. Like we did earlier, think of a descriptive name for your variable and write it down, slap an equals sign after it. Here's an example:
Hello_World =
Noticing that I put an underscore where I would put a space, remember, you're allowed to do this, just don't put a space, or it will error.
Now add speech or quotation marks to declare your variable's value as a string, in between the speech/quote marks write a value for your variable. Example.
Hello_World = "Hello World!"
In the example, the variable, Hello_World contains the value, Hello World!.
Now let's print the variable's value by doing exactly the same as we did before, but with the new variable's name.
print(Hello_World)
Click play, in the output, it will now print what you defined in your variable. So my output says Hello World!. Amazing huh?
Booleans
A Boolean, or a Bool, only contains two values, true and false. This is easier to teach, therefore, easier to learn and easier to master, than the others. Do what we did before, create a descriptive name, slap an equals sign.
Breathing =
Are you breathing, or are you not breathing? If you're not breathing, we can put a false, on the other hand, if you are breathing, put a true.
Breathing = true
Let's overview, we've declared a variable named Breathing and we are breathing, so we put a true after it. Do as we did before and type in print(Breathing), the output reads out true.
You're probably wandering what use this has to anything, but it is actually a pretty good invention as we will see later on in the guide.
Instance
We're at the end of page one in my How to Script guide, learning how to create an instance variable.
An instance variable is a little different to the others, this one contains the value of an object in your game. It can be in Workspace, Lighting, anywhere!
Do what we did last time, you know the drill:
My_object =
After the equals we put a path to an object. Let's say, I put a Part in Workspace and the part is named Part. So to find it using a script we would do this:
My_object = Workspace.Part
We have now indexed my object, Part. Using this we can simply edit Part's properties, for example:
My_object = Workspace.Part
My_object.Transparency = 1
The above example, firstly, would define Part in Workspace as My_object being an instance variable and would set it's Transparency Property to 1, in other words making it invisible.
We will learn about properties in the next page.
<- Previous Page | Next Page ->