Understanding Roblox Lua Scripting: A Starter Tutorial
DevelopmentGuides

Understanding Roblox Lua Scripting: A Starter Tutorial

Roblox uses Lua 5.1 as its scripting language, and it’s one of the most beginner-friendly languages in game development. This tutorial covers the fundamentals you need to start writing functional scripts in Roblox Studio.

Script Types

Roblox has three types of scripts: Script (runs on the server), LocalScript (runs on the client — each player’s device), and ModuleScript (a reusable library required by other scripts). Understanding which type to use is the most important foundational concept in Roblox development.

Variables and Data Types

local playerName = "Player1"   -- string
local score = 0                -- number
local isAlive = true           -- boolean
local character = nil          -- nil (empty)

The local keyword scopes your variable to the current block. Always use local unless you specifically need a global variable — global variables are accessible anywhere but are slower and harder to manage.

Control Flow

if score >= 100 then
    print("You won!")
elseif score >= 50 then
    print("Halfway there!")
else
    print("Keep going!")
end

Loops

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

-- Loop through a table
local items = {"sword", "shield", "potion"}
for _, item in ipairs(items) do
    print(item)
end

Your First Functional Script: A Touch-Activated Door

Place a Part in your game and insert a Script inside it. Paste this code:

local door = script.Parent
local isOpen = false

door.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and not isOpen then
        isOpen = true
        door.Transparency = 0.7
        door.CanCollide = false
        task.wait(3)
        door.Transparency = 0
        door.CanCollide = true
        isOpen = false
    end
end)

This script makes the door transparent and non-solid when a player touches it, then restores it after 3 seconds. Congratulations — you’ve written your first working Roblox script.

Leave a Comment