3.3 Indexing a Part

Let’s first, add a part using the object browser or the Model tool. That will be our target part.

Lua
local part = game.Workspace.Part

Just like the dot operator can be used to index the workspace, it can be used to index a part.

And also to access an object’s properties. If a property is not read-only, that property can be modified by assigning it a new value.

Lua
local part = game.Workspace.Part
print(part) --Output: Part
print(part.Name) --Output: Part
print(part.Material) --Output: Plastic
print(part.Mass) --Output: 5.6

part.Name = "Hello new part!"
print(part.Name) --Output: Hello new part!

The second to last line changes the name of the part. Afterwards, it will show up in the explorer as ‘Hello new part!’

One caveat to be aware of is name collisions between a child object and a property of the object when indexing with the dot operator.

If a child object shares the same name as a property, the dot operator will reference the property rather than the child object.

A different way to index a child object is by using the ‘FindFirstChild‘ method.

(Add a new part or change the part named “Hello new part!” back to “Part.”)

Lua
local part = game.Workspace:FindFirstChild("Part")

Every object has a ‘Parent‘ property that determines the location of the object in the game hierarchy. You can move the object to a new parent by reassigning this property:

Lua
local replicatedStorage = game:GetService("ReplicatedStorage")
local part = game.Workspace:FindFirstChild("Part")

part.Parent = replicatedStorage

This will move the part into ReplicatedStorage.