Skip to content

Roblox Luau Basics: Variables, Functions, and Events

Learn the small set of Luau building blocks you will see in almost every Roblox script.

Updated · 6 min read
On this page

You do not need to memorize every Roblox API. Learn how to store a value, make a decision, name a reusable action, and react to an event. Those four skills make most beginner scripts much easier to read.

Variables and values

A variable gives a value a name. local limits that name to its scope. Numbers, strings, and booleans are common starting points.

Variables.server.lua
local coins = 0local playerMessage = "Welcome!"local roundRunning = truecoins = coins + 1print(playerMessage, coins)

Conditions

An if statement runs code only when a condition is true. Use == to compare values; one = assigns a value. This example only prints a message—it does not award anything.

Conditions.server.lua
local coins = 12if coins >= 10 then    print("You have enough coins")end

Functions

A function groups steps under a name. Parameters are inputs; return sends a result back to the caller.

Functions.server.lua
local function addCoins(current, reward)    return current + rewardendlocal total = addCoins(10, 5)print(total) -- 15

Events

An event tells your code something happened. Connect attaches a function to that event. Put this Script inside an anchored Part, then touch it in Play mode:

Events.server.lua
local part = script.Parentpart.Touched:Connect(function(hit)    print("Touched by", hit.Name)end)

A touch can fire more than once. Add a cooldown when the action should happen at most once in a short period; the color brick tutorial shows how.

Practice one change

  1. Predict what a script will do before running it.
  2. Change one value and test your prediction.
  3. Use Output to inspect intermediate values.
  4. Explain the script in your own words before adding more code.
Keep rewards on the serverA client can request an action, but the server should validate whether it is allowed before changing currency or shared progress.

Build your next small idea.

Try the generator