Syntax
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspaceExamples
Configure before parenting
Script in ServerScriptService. Parenting last exposes a fully configured block to the scene. Destroy removes it after a short demonstration lifetime.
local part = Instance.new("Part")
part.Name = "TemporaryMarker"
part.Anchored = true
part.Position = Vector3.new(0, 6, 0)
part.Size = Vector3.new(2, 2, 2)
part.Parent = workspace
task.delay(5, function()
part:Destroy()
end)Narrow the class before changing a property
Script in ServerScriptService. Create a Folder named LessonParts in Workspace and put several Parts inside it. Only direct BasePart children are changed; nested descendants are intentionally excluded.
local folder = workspace:WaitForChild("LessonParts")
for _, child in folder:GetChildren() do
if child:IsA("BasePart") then
child.Color = Color3.fromRGB(70, 140, 220)
end
endBest practices
- Use colon syntax for instance methods such as part:Destroy().
- A destroyed instance should not remain the owner of later work; cancel or disconnect callbacks that no longer have a purpose.
- Choose GetChildren or GetDescendants deliberately; a recursive search can touch far more objects than intended.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
Events, connections and cleanup
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.game, workspace and storage services
game is the root DataModel and workspace holds the active 3D world. GetService obtains engine services by class name. ReplicatedStorage is visible to both sides; ServerStorage holds objects clients should not receive until the server places them into a replicated location.Humanoid, HumanoidRootPart and attributes
Humanoid controls character behaviour such as health, while HumanoidRootPart gives a useful spatial reference. Attributes attach small named values to Instances. They are convenient for configuration and display, but client-side attributes are not proof that a player earned a reward.Script, LocalScript and ModuleScript placement
The same code can behave differently depending on where it runs. These lessons use server Scripts in ServerScriptService and LocalScripts in StarterPlayerScripts or StarterGui. A ModuleScript runs when required. Script RunContext also affects execution, so inspect it when a script appears silent.
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.game, workspace and storage services
game is the root DataModel and workspace holds the active 3D world. GetService obtains engine services by class name. ReplicatedStorage is visible to both sides; ServerStorage holds objects clients should not receive until the server places them into a replicated location.Humanoid, HumanoidRootPart and attributes
Humanoid controls character behaviour such as health, while HumanoidRootPart gives a useful spatial reference. Attributes attach small named values to Instances. They are convenient for configuration and display, but client-side attributes are not proof that a player earned a reward.Script, LocalScript and ModuleScript placement
The same code can behave differently depending on where it runs. These lessons use server Scripts in ServerScriptService and LocalScripts in StarterPlayerScripts or StarterGui. A ModuleScript runs when required. Script RunContext also affects execution, so inspect it when a script appears silent.