- Installing Roblox Studio
- Let's Take a Tour
- Opening the Output Window
- Writing Your First Script
- Error Messages
- Leaving Yourself Comments
- Summary
- Q and A
- Workshop
- Exercise
Writing Your First Script
On to coding! You need something to hold your code, and that’s a script. You can insert scripts directly into objects within the world. In this case, you’re inserting a script into a part.
Insert a Script into a Part
A part is the basic building block of Roblox. Parts can range in size from very tiny to extremely large. They can be different shapes such as a sphere or wedge, or they can be combined into more complex shapes.
Return to the Home tab and click Part (see Figure 1.8). The part appears in the 3D Editor at the center of your camera view.
FIGURE 1.8 Click Part on the Home tab to insert a part.
To add a script, in Explorer, hover over the part and click the + symbol, and then select Script from the drop-down menu (see Figure 1.9).
FIGURE 1.9 You use Explorer to insert a script into the part.
The script automatically opens. At the top, you see words familiar to any coder: "Hello world!" (see Figure 1.10).
FIGURE 1.10 The window shows the default script and code.
Writing Some Code
Since the 1970s, "Hello World!" has been one of the first pieces of code people have learned. Here it’s being used in the print function. Functions are chunks of code that serve a specific purpose. As you learn to code, you’ll use prebuilt functions like print(), which displays messages in the Output window. You will, of course, also learn how to create functions of your own.
print() displays a string, which is a type of data usually used with letters and numbers that need to stay together. In this case, you’re printing "Hello world!":
Make this code your own by changing the message inside of the quotation marks to what you want for dinner tonight. Here’s an example:
print("I want lots of pasta")
To test the code, in the Home tab, click Play (see Figure 1.11).
FIGURE 1.11 Click Play to test your script.
Your avatar will fall into the world, and you can see your dinner dreams displayed in the Output window, along with a note about which script that message came from (see Figure 1.12).
FIGURE 1.12 The string is displayed in Output.
To stop the playtest, click the Stop button (see Figure 1.13).
FIGURE 1.13 Click Stop to quit the playtest.
Return to your script by clicking on the tab above the 3D Editor, as shown in Figure 1.14.
FIGURE 1.14 Click Script to return to the window where your script is visible.
Code an Explosion
Code of course can do more than just display messages to the output window. It can completely change how players interact with the world and make it come alive. Let’s take a slightly longer piece of code and make the block in the Baseplate template destroy anything it touches:
Use the Move tool (see Figure 1.15) to move the block off the ground and away from the spawn point. The code you’re going to write will destroy anything it touches, and you don’t want it to go off prematurely.
FIGURE 1.15 Move the part up and away from the spawn.
In the Properties window, scroll to Behavior and make sure Anchored (see Figure 1.16) is selected so the block doesn’t fall when you click Play.
FIGURE 1.16 Check Anchored to keep the blocks from falling.
In the script, below the print function, add the following code:
print("I want lots of pasta!") -- Destroys whatever touches the part local trap = script.Parent local function onTouch(partTouched) partTouched:Destroy() end trap.Touched:Connect(onTouch)
Click Play and run up and touch the part.
The result should be that your character breaks or parts of your avatar are destroyed. You may notice that this code only destroys what touches it directly, such as your feet. Try jumping on top of the block or brushing against it with just a hand. You’ll see only that part of your avatar is destroyed.
The reason is that code only does what you tell it, and you told the part to destroy only what it touches and nothing more. You have to tell it how to destroy the rest of the player. Throughout this book, you’ll learn how to write additional instructions so that the code can handle more scenarios like this one. In Hour 4, “Parameters and Arguments,” you’ll learn how to make sure it destroys the entire player character.