PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Properties and BrickColor

Properties and BrickColor

Prerequisites
  • General knowledge of Studio covered in Intro to Studio
  • Understand decimal values
Lesson Time 5 - 10 minutes
Optional Handouts Coding with Lua
Learning Objectives
  • Use comments to describe code in plain language
  • Create a script to change a part's color using RBG values

All objects in Roblox have properties that control how they look and function, such as material, color, and shape. You can use code to change these properties to make things happen in-game. By changing the color property of an object you can create a disco floor or making flashing buttons.

Take any available object and have your class describe at least three properties of that object. Emphasize a mix of properties that are visual and functional.

Pencil Example:

  • Color: yellow
  • Edible: False
  • Makes marks: Yes
  • Line color: grey

Explore Properties

To get an idea of some properties already being used by objects in your game:

  1. Open a previous project or create a new one.
  2. Create and click on a new part.
  3. Scroll through the Properties Window on the bottom right.

Many of an object’s properties are shown in the Properties window. You can use the Properties window to change properties before a game starts. However you need to use code to change properties like color or visibility in-game.

Can't see the Properties Window?

It's possible for the Property Window to be accidentally closed. To re-open the window, click on the View Tab and then click on the Properties button.

Changing Properties

The script will use code to change the color of a part at the start of the game.

Set up the Part and Script

  1. Select an existing part or create a new one.
  2. Rename the part. This example uses PracticePart.
  3. Rename the script ChangeBrickColor.
  4. Delete the the Hello World line.

Creating Comments

You should always start new scripts off with a comment about what the script does. Comments are special lines that help coders remember what scripts are for but doesn’t give Roblox Studio instructions.

Create Your First Comment

  1. Type -- and a note about what this script does. The text should turn green to let you know it’s a comment.

Coding a Part to Change Color

While you know the exact part in Studio you want to change, the program doesn’t know yet. Each script needs instructions where to find a specific part and what property to change.

Start at the Top

To find the part, the script will look in game, where the Workspace and part will be.

  1. Type game below your comment.

Find the Part in the Explorer

Remember that the Explorer Window lists all of the objects in your game and shows how they’re connected to each other. Notice in the Explorer that PracticePart is under Workspace.

Now that you know PracticePart is under Workspace, you can turn this information into lines of code that the program can understand.

Coding Vocabulary - Parent and Child

Parent and child mean something specific in programs. Parts that are under the Workspace are said to be children of the Workspace, while the Workspace is the part's parent The relationship is like below:

  • game > Workspace > PracticePart

Write a Line with Dot Notation

You still have to give the program directions to the part, but if the words are typed all together, like gameWorkspacePracticePart, the program will think it’s one big word instead of separate words.

So the program can recognize each word, objects and properties are separated using a dot. Always type the names exactly as you see them and don’t use any extra spaces.

  1. On the same line as game, type .Workspace
  2. Type . followed by the NameOfYourPart you’re using.
--Changes color of PracticePart
game.Workspace.PracticePart

Check Your Code Before Moving on

Make sure your code looks exactly like the code above, and that NameOfYourPart is spelled and capitalized exactly like in the Explorer.

Using Autocomplete

Roblox will autocomplete words as you type to help speed up the coding process. When the words appear, you can use the arrow keys to move down the list. Pick an option by pressing Enter.

Changing a Property with Code

Almost there! Now, you’ll change the part’s color with the BrickColor property.

To start changing the color:

  1. Type .BrickColor after the name of your part.
--Changes color of PracticePart
game.Workspace.PracticePart.BrickColor

It's important for new students to check their code every few steps to reduce the possibility of errors. Additionally, encourage students to identify and solve their own errors (or ask their peers). This will help students become more independent while troubleshooting.

When checking their work, ask students the following:

  • Is the capitalization correct?
  • Is everything spelled correctly?
  • Are there any extra spaces?

Using RGB Values

To change the BrickColor property, you’re going to create a new BrickColor that will replace the current one. It’s not like mixing paints though, programs use RGB values, the combination of red, green, and blue to create all the colors on your screen.

There are some rules for using RBG values:

  • Use 3 decimal numbers; one for each color.
  • Separate each number with a comma.
  • Use numbers between 0 and 1. 0 means a color is all the way off. 1 means the color is all the way on.

Below are some examples of RGB values:

Color Code
BrickColor.new(0.9,0.6,0.2)
BrickColor.new(0.2,0.6,0.2)
BrickColor.new(0.8,0.2,0.8)

Create a new RGB Color

You’ll now use the = sign to set a new color to replace the original part color. When creating a color, you can use the decimal numbers for one in the table above, or come up with your own.

  1. Type the following after game.Workspace.PracticePart.BrickColor:

    = BrickColor.new()

  2. Inside the parenthesis, add 3 different decimal numbers (between 0 and 1), separated by commas.

  3. Run Studio; your brick color should now change!

-- Changes color of PracticePart
game.Workspace.PracticePart.BrickColor = BrickColor.new(0.9,0.8,0.1)

Troubleshooting Your Code

Remember that errors are a natural part of the coding process. Try the following if you have any errors (like a red line in the Script Editor or error message in the Output Window).

Issue: Part is still gray or doesn’t change color as intended

  • Make sure you followed all three rules for RGB values (number is 0-1, is a decimal, all numbers separated by commas).
  • If you are doing random numbers, you may get a surprise color.

Troubleshooting Tips

  • Double check that the capitalization and spelling is the same as the code example. Brickcolor will not work, while BrickColor will work.

These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.