Creating a Script

This is an easy tutorial This is an easy tutorial. < Creating Parts via Code Making a Day/Night Cycle > This tutorial is part of a series on scripting. Return to tutorial index



In the previous tutorials we have been inserting code commands one at a time to our game to change properties through the Command Bar. In this tutorial we will cover adding a Script to our game.

Adding a Script[edit]

Instead of giving instructions one at a time to the game through the Command Bar, we can provide a list of instructions by way of a special object called a script. A script is just like a text file where we can type as many instructions as we want and the game will execute them in order one at a time. Unlike the Command Bar however, a script will not run until a game is started.

Lets start by adding a script that changes color and transparency of the Baseplate. To add a script, right click on ServerScriptService, hover over Insert Object and click on Script. The script will automatically open a new view called the Script Editor. By default it will have a basic print statement in it:

print 'Hello world!'

Delete the existing code and replace it with the following:

game.Workspace.Baseplate.Transparency = 0.5
game.Workspace.Baseplate.BrickColor = BrickColor.Blue()

If you close the script or click on the tab for your game, you'll notice that nothing has changed yet in the game. This is because the script will not be executed until the game runs. We can start the game using the Play button, but for testing scripts there is another play mode that is more convenient. Click on the arrow underneath the Play button and click on Run. This will start the game without inserting a character.

Scripting3 RunButton.png

After pressing Run, you'll see that the Baseplate becomes blue and translucent. Pressing Stop will revert the changes the script made and bring you back to the edit mode.

Script Parent[edit]

Most of the time Scripts should go into ServerScriptService. However, there are some cases where it is convenient to put Scripts elsewhere. For example, lets consider a script that changes the color of a brick to a random color. Normally the script would be in ServerScriptService and would look something like this:

game.Workspace.Part.BrickColor = BrickColor.Random()

But what if we had a lot of parts we wanted to change the color of? If all of the parts are in the Workspace and we wanted to use a script like above, we would have to name all the parts differently and write out a command for each:

game.Workspace.Part1.BrickColor = BrickColor.Random()
game.Workspace.Part2.BrickColor = BrickColor.Random()
game.Workspace.Part3.BrickColor = BrickColor.Random()
game.Workspace.Part4.BrickColor = BrickColor.Random()
game.Workspace.Part5.BrickColor = BrickColor.Random()

There is actually a much easier way to do this! Instead of inserting one script inside ServerScriptService, create a new script inside one of the parts. Inside the script put the following code:

script.Parent.BrickColor = BrickColor.Random()

script is a special keyword referring to the Script the code is currently in. Writing script.Parent refers to the Parent property of the Script. If the Script is inside a part, the script.Parent will refer to that part.

In the Explorer we can now copy and paste this new script and put it inside any part we want to change the color of. This way we do not have to give a unique Name to any of the parts as each Script will change the color of whatever part the Script is inside.

In the next tutorial we will learn how to use loops and conditional statements to create a day/night cycle.

This is an easy tutorial This is an easy tutorial. < Creating Parts via Code Making a Day/Night Cycle > This tutorial is part of a series on scripting. Return to tutorial index