Skip to main content
Featured Guide

Roblox Scripting for Beginners (Lua Basics)

Learn Lua scripting for Roblox from scratch. This beginner's guide covers variables, functions, events, and your first working scripts in Roblox Studio.

BloxRedeem Team January 23, 2026

Want to make your Roblox games interactive? You need to learn scripting. Roblox uses a programming language called Lua (specifically, Luau—Roblox’s enhanced version). This guide teaches you the fundamentals from scratch.

No coding experience needed. By the end, you’ll write working scripts.

What Is Scripting?

Scripts are instructions that tell your game what to do:

  • Open a door when touched
  • Give players coins when they collect items
  • Spawn enemies every 30 seconds
  • Create custom weapons and abilities

Without scripts, your Roblox game is just static objects.


Setting Up Roblox Studio

Before scripting, you need Roblox Studio.

Installation

  1. Go to create.roblox.com
  2. Click Start Creating
  3. Download and install Roblox Studio
  4. Log in with your Roblox account

Creating a New Place

  1. Open Roblox Studio
  2. Click NewBaseplate (simple flat world)
  3. You’re ready to script!

Your First Script

Let’s write code that prints a message.

Creating a Script

  1. In the Explorer panel (right side), find ServerScriptService
  2. Right-click → Insert ObjectScript
  3. Double-click the new script to open it

The Default Code

New scripts start with:

print("Hello world!")

Running Your Script

  1. Click Play (or press F5)
  2. Open the Output panel (View → Output)
  3. You’ll see: Hello world!

Congratulations—you just ran your first script!


Understanding Lua Basics

Variables

Variables store information:

local playerName = "BloxMaster"
local playerLevel = 10
local isVIP = true
  • local declares a variable
  • = assigns a value
  • Strings use quotes: "text"
  • Numbers don’t: 10
  • Booleans are true or false

Printing Variables

local coins = 100
print(coins)  -- Output: 100
print("You have " .. coins .. " coins")  -- Output: You have 100 coins

The .. operator combines (concatenates) text.

Basic Math

local a = 10
local b = 5

print(a + b)  -- 15
print(a - b)  -- 5
print(a * b)  -- 50
print(a / b)  -- 2
print(a ^ 2)  -- 100 (power)
print(a % 3)  -- 1 (remainder)

Conditional Statements

Make decisions in code with if statements:

local age = 15

if age >= 18 then
    print("You are an adult")
else
    print("You are not an adult")
end

Comparison Operators

OperatorMeaning
==Equal to
~=Not equal to
>Greater than
<Less than
>=Greater or equal
<=Less or equal

Multiple Conditions

local score = 85

if score >= 90 then
    print("A grade")
elseif score >= 80 then
    print("B grade")
elseif score >= 70 then
    print("C grade")
else
    print("Keep trying!")
end

Combining Conditions

local level = 10
local hasKey = true

if level >= 5 and hasKey then
    print("You can enter the dungeon")
end

if level < 5 or not hasKey then
    print("Access denied")
end
  • and – Both must be true
  • or – At least one must be true
  • not – Inverts the condition

Loops

Repeat code multiple times.

For Loop

for i = 1, 5 do
    print("Count: " .. i)
end

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

While Loop

local count = 0

while count < 3 do
    print("Looping...")
    count = count + 1
end

Warning: Infinite loops crash your game! Always have an exit condition.

Looping Through Tables

local fruits = {"Apple", "Banana", "Cherry"}

for index, fruit in ipairs(fruits) do
    print(index .. ": " .. fruit)
end

Output:

1: Apple
2: Banana
3: Cherry

Functions

Functions are reusable blocks of code:

local function greet(name)
    print("Hello, " .. name .. "!")
end

greet("Alex")   -- Output: Hello, Alex!
greet("Jordan") -- Output: Hello, Jordan!

Returning Values

local function add(a, b)
    return a + b
end

local result = add(5, 3)
print(result)  -- Output: 8

Why Use Functions?

  • Avoid repeating code
  • Organize your scripts
  • Make changes in one place

Working With Parts

Now let’s interact with the game world.

Finding a Part

If you have a part named “MyPart” in Workspace:

local part = game.Workspace.MyPart
print(part.Name)  -- Output: MyPart

Changing Properties

local part = game.Workspace.MyPart

part.Color = Color3.fromRGB(255, 0, 0)  -- Red
part.Size = Vector3.new(10, 1, 10)        -- Make it bigger
part.Position = Vector3.new(0, 10, 0)     -- Move it up
part.Transparency = 0.5                    -- Half transparent
part.Anchored = true                       -- Won't fall

Destroying a Part

local part = game.Workspace.MyPart
part:Destroy()

Events

Events trigger code when something happens.

Touched Event

Run code when a player touches a part:

local part = game.Workspace.TouchPart

local function onTouched(hit)
    print("Something touched the part!")
    print(hit.Name)  -- Name of what touched it
end

part.Touched:Connect(onTouched)

Getting the Player

Usually, you want to know which player touched:

local part = game.Workspace.TouchPart

local function onTouched(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    
    if player then
        print(player.Name .. " touched the part!")
    end
end

part.Touched:Connect(onTouched)

Other Common Events

EventWhen It Fires
TouchedSomething touches the part
ClickDetector.MouseClickPlayer clicks the part
Players.PlayerAddedA player joins the game
Players.PlayerRemovingA player leaves the game
Humanoid.DiedA player dies

Practical Examples

Example 1: Kill Brick

A part that kills players on touch:

local killBrick = game.Workspace.KillBrick

local function onTouched(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end

killBrick.Touched:Connect(onTouched)

Example 2: Color-Changing Part

A part that changes color every second:

local part = game.Workspace.ColorPart

while true do
    part.Color = Color3.fromRGB(
        math.random(0, 255),
        math.random(0, 255),
        math.random(0, 255)
    )
    wait(1)
end

Example 3: Door That Opens

A door that opens when touched:

local door = game.Workspace.Door
local debounce = false

local function onTouched(hit)
    if debounce then return end
    debounce = true
    
    -- Open the door (move it up)
    door.Position = door.Position + Vector3.new(0, 10, 0)
    
    wait(3)
    
    -- Close the door
    door.Position = door.Position - Vector3.new(0, 10, 0)
    
    debounce = false
end

door.Touched:Connect(onTouched)

The debounce prevents the code from running multiple times simultaneously.

Example 4: Welcome Message

Greet players when they join:

local function onPlayerAdded(player)
    print("Welcome, " .. player.Name .. "!")
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

Script Types

Server Scripts vs. Local Scripts

TypeWhere It RunsUse For
ScriptServerGame logic, data, NPCs
LocalScriptClient (player’s device)UI, camera, local effects

Rule: Anything that affects all players = Server Script Anything specific to one player’s view = Local Script

Where to Put Scripts

Script TypeLocation
Server ScriptServerScriptService
LocalScriptStarterPlayerScripts, StarterGui
ModuleScriptAnywhere (shared code)

Common Mistakes

Nil Errors

local part = game.Workspace.MissingPart  -- Doesn't exist!
print(part.Name)  -- Error: attempt to index nil

Fix: Check if something exists before using it:

local part = game.Workspace:FindFirstChild("MissingPart")
if part then
    print(part.Name)
end

Infinite Loops

while true do
    print("This crashes your game!")
end

Fix: Add wait() in loops:

while true do
    print("This is safe")
    wait(1)  -- Pause for 1 second
end

Forgot to Connect Event

local function onTouched(hit)
    print("Touched!")
end
-- Forgot: part.Touched:Connect(onTouched)

Nothing happens because the function is defined but never connected.


Debugging Tips

Use Print Statements

Add prints to track what’s happening:

print("Script started")
local part = game.Workspace.MyPart
print("Part found: " .. tostring(part))

Check the Output Panel

View → Output shows:

  • Print messages
  • Errors (in red)
  • Warnings (in yellow)

Read Error Messages

Workspace.Script:5: attempt to index nil with 'Name'

This tells you:

  • Location: Workspace.Script:5 (line 5)
  • Problem: Tried to use .Name on something that doesn’t exist (nil)

Next Steps

Once comfortable with basics:

  1. Learn RemoteEvents – Communicate between server and client
  2. Explore DataStores – Save player data
  3. Study TweenService – Smooth animations
  4. Understand OOP – Object-oriented programming patterns
  5. Use ModuleScripts – Organize large projects

Resources


Summary

Lua scripting basics:

  1. Variables – Store data (local x = 10)
  2. Conditionals – Make decisions (if, elseif, else)
  3. Loops – Repeat code (for, while)
  4. Functions – Reusable code blocks
  5. Events – Respond to actions (.Touched, .PlayerAdded)
  6. Properties – Change parts (.Color, .Size, .Position)

Start simple, build complexity gradually. Every great Roblox developer started with “Hello world!”


Ready to build your first game? Check out our Roblox Studio Basics guide for the complete development workflow!

roblox scripting lua programming studio developer coding