Roblox Luau Tutorial: Understanding the Parent-Child Relationship
Learn how objects interact within Roblox's hierarchy (DataModel / Game) and how to write scripts to modify an object's properties dynamically.
1. What is the Parent-Child Relationship?
- Parent: Any object, container, service, or DataModel that holds other items inside it.
- Example: The DataModel (
game) is the ultimate top-level container.
- Example: The DataModel (
- Child: Any object, script, or service contained inside another object.
- Example:
Workspaceis a child ofgame. APartplaced insideWorkspaceis a child ofWorkspace.
- Example:
Hierarchy Structure
game (DataModel / Parent)
└── Workspace (Child of game / Parent of Part)
└── Part (Child of Workspace / Parent of Script)
└── Script (Child of Part)
2. Setting Up the Scene in Roblox Studio
- Insert a Part:
- Press
Ctrl + I(Windows) orCmd + I(Mac) to open the Insert Object menu. - Search for Part and select it.
- Press
- Insert a Script inside the Part:
- Press
Ctrl + I/Cmd + Iagain while selecting thePart. - Search for Script and place it inside the
Part.
- Press
3. Methods to Access Parents and Children in Luau
Method 1: The Parent Property (Direct Reference)
Since the script is placed directly inside the Part, referencing script.Parent gives direct access to that Part.
-- Accessing the parent directly from the script
local part = script.Parent
Method 2: The Child Method (Path Reference)
Accessing an object by navigating down through the DataModel hierarchy (game.Workspace.Part).
-- Accessing the part via the DataModel path
local part = game.Workspace.Part
Method 3: Function Methods (FindFirstChild & FindFirstAncestor)
Using built-in Roblox functions to search up or down the tree safely.
-- Search upward through parents for an object named "Part"
local part = script:FindFirstAncestor("Part")
-- Search downward inside Workspace for a child named "Part"
local part = workspace:FindFirstChild("Part")
4. Modifying Object Properties via Code
Using script.Parent, you can dynamically change property values such as Name, BrickColor, and Transparency:
-- Reference the Part parented to this script
local part = script.Parent
-- 1. Change the Name property
part.Name = "John Doe"
-- 2. Change the Color property
part.BrickColor = BrickColor.new("Bright green")
-- 3. Change the Transparency property
part.Transparency = 0.5
5. Verification / Testing
- Click Run in Roblox Studio.
- Inspect the Part in the Workspace:
- Color: Has changed to bright green.
- Transparency: Is visually semi-transparent (
0.5). - Explorer Name: Updated to
"John Doe".