Get started with Roblox-specific functions and the game hierarchy.
Roblox games are organized in a tree structure with game as the root.
-- Get important services
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Shorter way for Workspace
local workspace = game.Workspace -- or just 'workspace'
Services are the top-level containers in Roblox. Use GetService() for best practice.
Parts are the basic building blocks in Roblox.
-- Create a new part
local part = Instance.new("Part")
part.Size = Vector3.new(10, 5, 10)
part.Position = Vector3.new(0, 10, 0)
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.Neon
part.Parent = workspace
-- Make it anchored (won't fall)
part.Anchored = true
Always set Parent last to avoid performance issues!
-- Find by name
local myPart = workspace:FindFirstChild("PartName")
if myPart then
print("Found the part!")
myPart.Transparency = 0.5
end
-- Get all children
for _, child in ipairs(workspace:GetChildren()) do
print(child.Name)
end
Always check if FindFirstChild() returns nil before using it!
Access and manipulate player data.
local Players = game:GetService("Players")
-- When a player joins
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined the game!")
end)
-- When a player leaves
Players.PlayerRemoving:Connect(function(player)
print(player.Name .. " left the game!")
end)
Events use :Connect() to run functions when something happens.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
-- Wait for character to load
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
print(player.Name .. "'s health: " .. humanoid.Health)
end)
end)
Use WaitForChild() to ensure parts exist before accessing them.
Create a blue neon part in the workspace.
-- Create your part here
local part = Instance.new("Part")
part.Size = Vector3.new(5, 1, 5)
part.Position = Vector3.new(0, 5, 0)
part.BrickColor = BrickColor.new("Really blue")
part.Material = Enum.Material.Neon
part.Anchored = true
part.Parent = workspace
Print a welcome message when players join.
local Players = game:GetService("Players")
-- Write your PlayerAdded event here
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
print("Welcome to the game, " .. player.Name .. "!")
end)
Continue your learning journey