We use cookies on this site to enhance your user experience

Creating Parts via Code

Creating Parts via Code

Jul 03 2018, 11:22 AM PST 5 min

In this tutorial, we will start to explore how to use code to make more things happen in Roblox Studio than just printing text in the Output window. We will learn how to create a new Part through code and how to change its properties.

Creating a new Part

Until now, we have been using the Part button to insert new parts into our game. However, we can also create a new Part via code. To do this, type the following command:

Instance.new("Part", workspace)

This will insert a new Part in the Workspace. This part will show up in both the 3D view and the Explorer.

Scripting2_Part3D.png Scripting2_PartWorkspace.png

Just like with strings and numbers, we can also store Parts inside variables. Lets insert a new part in to the game and save it in a variable called myPart:

myPart = Instance.new("Part", workspace)

As with other variables, we can print this variable and see that it is storing a part:


Changing Properties

Now that we have a variable with a part stored in it, we can change the properties of that part. Any of the properties that you can set with Studio tools or through the Properties window can be changed with a script command. To change a property of something stored in a variable we have to provide the variable, the property we want to change, and the value we want the property changed to. In code this has to be presented in a very specific way. When accessing a property, you have to include a dot (.) between the variable and the property name. For example, to change the name of our part we can write the following:

myPart.Name = "Bob"

After entering this command we can see the name of the part in the explorer changed from Part to Bob.


We can change other properties as well. For example, if we want to make a part slightly transparent, we can change the Transparency property.

myPart.Transparency = 0.5


What about parts that already exist in the game? We can change those with code too. When we need to do something to an existing part in the game, we have to specify how to get there through the Explorer. To do this, we must first understand how games in Roblox are organized.

A Roblox game is structured into what is called a tree. The very top of
the tree is the game itself (referred to in code as game). Underneath the game are the standard services such as Workspace, Players, Lighting, etc. Since these are a level below game, they are considered game’s “Children”. Likewise, game is considered these services** Instance/Parent. If you go down one more level, you’ll notice that Workspace also has several Children. In an empty place, it typically has three: Terrain, Camera, and Baseplate. If you add anything else in the 3D view (like a Part, Decal, etc.), these will become children of Workspace.


Looking at the Explorer is basically like looking at this tree on its side. The game is always hidden so all of it’s children appear on the first level of the Explorer. Anytime something is indented in the Explorer, it is a Child of whatever it is under.


Now that we know how the game is organized, we can talk about how to access an object in the Explorer through code. When we want to get to a specific object, we have to write how to get there via the tree. For example, suppose we want to change the Transparency of the Baseplate. We know the Baseplate is a child of Workspace, which itself is a child of game. To access an object in code, we start at the highest level and then work down through the children. So, we start at game as that is the highest level in the tree.


To move to a child, we have to write dot (.) then the name of the child. In this case, we want to move to Workspace so we would write:


We can descend another level to the Baseplate by writing another dot (.) and then Baseplate:


Now that we are at the Part we are interested in, we can access the Property we are interested (with a dot, just like above), and then set it.

game.Workspace.Baseplate.Transparency = 1
  • part