Luau Cheat Sheet

Quick reference for Roblox Luau scripting. Click any code block to copy.

Variables & Types

Local Variables
local name = "Steve" local score = 100 local alive = true local nothing = nil
Type Annotations
local name: string = "Steve" local score: number = 100 local alive: boolean = true local items: {string} = {"Sword", "Shield"}
Type Checking
print(type(name)) -- "string" print(type(score)) -- "number" print(type(alive)) -- "boolean" print(typeof(part)) -- "Instance"
Type Casting
local num = tonumber("42") -- 42 local str = tostring(42) -- "42"

Operators

Arithmetic
local a = 10 + 5 -- 15 local b = 10 - 5 -- 5 local c = 10 * 5 -- 50 local d = 10 / 5 -- 2 local e = 10 % 3 -- 1 (modulo) local f = 10 ^ 2 -- 100 (power) local g = -a -- -15 (negate)
Comparison
a == b -- equal a ~= b -- not equal a > b -- greater than a < b -- less than a >= b -- greater or equal a <= b -- less or equal
Logical
a and b -- true if both true a or b -- true if either true not a -- inverts boolean -- Short circuit: local x = a or defaultVal local y = a and b or c
String & Length
local full = "Hello" .. " World" local len = #"Hello" -- 5 local tblLen = #{1, 2, 3} -- 3

Control Flow

If / Elseif / Else
if score > 100 then print("High score!") elseif score > 50 then print("Good score") else print("Keep trying") end
Numeric For Loop
-- for start, end, step do for i = 1, 10 do print(i) end for i = 10, 1, -1 do print(i) -- countdown end
Generic For (ipairs / pairs)
local fruits = {"Apple", "Banana", "Cherry"} for i, fruit in ipairs(fruits) do print(i, fruit) end local stats = {HP = 100, MP = 50} for key, val in pairs(stats) do print(key, val) end
While & Repeat
while health > 0 do takeDamage() end repeat tryConnect() task.wait(1) until connected == true
Continue & Break
for i = 1, 10 do if i == 5 then continue -- skip 5 end if i == 8 then break -- stop at 8 end print(i) end

Functions

Basic Function
local function greet(name: string) print("Hello, " .. name) end greet("Player1")
Return Values
local function add(a: number, b: number): number return a + b end local function swap(a, b) return b, a -- multiple returns end local x, y = swap(1, 2)
Anonymous & Callbacks
local square = function(x) return x * x end button.Activated:Connect(function() print("Clicked!") end)
Variadic Functions
local function sum(...): number local total = 0 for _, v in ipairs({...}) do total += v end return total end print(sum(1, 2, 3, 4)) -- 10

Tables

Arrays
local arr = {10, 20, 30} print(arr[1]) -- 10 (1-indexed) table.insert(arr, 40) table.remove(arr, 2) print(#arr) -- length
Dictionaries
local player = { Name = "Steve", Level = 5, HP = 100, } print(player.Name) print(player["Level"]) player.HP = 80
Table Methods
table.insert(t, value) -- append table.insert(t, 1, value) -- insert at index table.remove(t, index) -- remove at index table.sort(t) -- sort in place table.find(t, value) -- find index table.clear(t) -- remove all table.clone(t) -- shallow copy table.freeze(t) -- make read-only
Nested Tables
local inventory = { weapons = {"Sword", "Bow"}, armor = {"Shield", "Helmet"}, } print(inventory.weapons[1]) -- "Sword"

String Methods

Common String Functions
string.len("Hello") -- 5 string.upper("hello") -- "HELLO" string.lower("HELLO") -- "hello" string.reverse("abc") -- "cba" string.rep("ha", 3) -- "hahaha"
Substring & Find
string.sub("Hello", 1, 3) -- "Hel" string.sub("Hello", 3) -- "llo" string.find("Hello", "ll") -- 3, 4 string.match("v1.2", "%d+") -- "1"
Format & Split
string.format("HP: %d/%d", 80, 100) -- "HP: 80/100" string.format("%.2f", 3.14159) -- "3.14" string.split("a,b,c", ",") -- {"a", "b", "c"}
String Interpolation
local name = "Steve" local level = 10 local msg = `Welcome {name}, level {level}!` -- "Welcome Steve, level 10!"

Math Methods

Rounding & Limits
math.floor(4.7) -- 4 math.ceil(4.2) -- 5 math.round(4.5) -- 5 (Luau extension) math.abs(-5) -- 5 math.max(1, 5, 3) -- 5 math.min(1, 5, 3) -- 1 math.clamp(15, 0, 10) -- 10
Random
math.random() -- 0 to 1 math.random(10) -- 1 to 10 math.random(5, 20) -- 5 to 20 -- Better randomness: local rng = Random.new() rng:NextInteger(1, 100) rng:NextNumber(0, 1)
Trigonometry
math.pi -- 3.14159... math.sin(angle) math.cos(angle) math.atan2(y, x) math.rad(degrees) -- to radians math.deg(radians) -- to degrees math.sqrt(16) -- 4
Interpolation
-- Linear interpolation (lerp) local function lerp(a, b, t) return a + (b - a) * t end lerp(0, 100, 0.5) -- 50 lerp(0, 100, 0.25) -- 25

Common Services

Getting Services
local Players = game:GetService("Players") local RS = game:GetService("ReplicatedStorage") local SS = game:GetService("ServerStorage") local SSS = game:GetService("ServerScriptService") local TS = game:GetService("TweenService") local UIS = game:GetService("UserInputService") local RunService = game:GetService("RunService") local Debris = game:GetService("Debris") local DS = game:GetService("DataStoreService") local HTTP = game:GetService("HttpService")
Workspace
workspace:FindFirstChild("PartName") workspace:GetChildren() workspace:GetDescendants() workspace:WaitForChild("Model") workspace.Gravity = 196.2 workspace:Raycast(origin, direction)
Players Service
Players.PlayerAdded:Connect(function(plr) print(plr.Name .. " joined") end) Players.PlayerRemoving:Connect(function(plr) print(plr.Name .. " left") end) local allPlayers = Players:GetPlayers() local plr = Players:FindFirstChild("Steve")
DataStoreService
local ds = DS:GetDataStore("PlayerData") -- Save data ds:SetAsync("key_" .. plr.UserId, { Coins = 100, Level = 5, }) -- Load data local data = ds:GetAsync("key_" .. plr.UserId) -- Update atomically ds:UpdateAsync("key", function(old) old = old or {} old.Coins += 10 return old end)

Common Events

Touch & Proximity
part.Touched:Connect(function(hit) local player = Players:GetPlayerFromCharacter( hit.Parent ) if player then print(player.Name .. " touched!") end end) part.TouchEnded:Connect(function(hit) print("Touch ended") end)
RemoteEvents
-- Server: local remote = RS:WaitForChild("MyEvent") remote.OnServerEvent:Connect(function(plr, data) print(plr.Name, data) end) -- Client: local remote = RS:WaitForChild("MyEvent") remote:FireServer("hello")
Input Events
UIS.InputBegan:Connect(function(input, gpe) if gpe then return end if input.KeyCode == Enum.KeyCode.E then print("E pressed") end end) UIS.InputEnded:Connect(function(input, gpe) if input.KeyCode == Enum.KeyCode.E then print("E released") end end)
RunService Loops
-- Runs every frame (client) RunService.RenderStepped:Connect(function(dt) -- update camera, UI, etc. end) -- Runs every physics step RunService.Heartbeat:Connect(function(dt) -- game logic end) -- Runs before physics RunService.Stepped:Connect(function(time, dt) -- pre-physics end)

Common Patterns

Debounce
local debounce = false part.Touched:Connect(function(hit) if debounce then return end debounce = true -- Do something once print("Touched!") task.wait(2) -- Cooldown debounce = false end)
Protected Calls (pcall)
local success, result = pcall(function() return ds:GetAsync("key_123") end) if success then print("Data:", result) else warn("Error:", result) end
Module Scripts
-- ModuleScript (in ReplicatedStorage) local MyModule = {} function MyModule.greet(name: string) return "Hello, " .. name end return MyModule -- Usage in another script: local MyModule = require(RS:WaitForChild("MyModule")) print(MyModule.greet("Steve"))
Tweening
local info = TweenInfo.new( 1, -- duration Enum.EasingStyle.Quad, -- style Enum.EasingDirection.Out, -- direction 0, -- repeat count false, -- reverses 0 -- delay ) local tween = TS:Create(part, info, { Position = Vector3.new(0, 10, 0), Transparency = 0.5, }) tween:Play()
Leaderstats
Players.PlayerAdded:Connect(function(plr) local ls = Instance.new("Folder") ls.Name = "leaderstats" ls.Parent = plr local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Value = 0 coins.Parent = ls end)
Spawn & Respawn
Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) local humanoid = char:WaitForChild("Humanoid") humanoid.Died:Connect(function() print(plr.Name .. " died") task.wait(5) plr:LoadCharacter() end) end) end)
Copied to clipboard