Skip to content
deltascripts.
Blog

Lua and Luau: Differences That Matter When Reading Roblox Code

Read three original code examples to recognize Luau types, compound assignments and interpolation, and separate language features from Roblox APIs.

By deltascriptsco-admin Published 4 min read
Related Lua and Luau notebooks beside a blocky game model

A Lua tutorial can look familiar right up to the line that won’t parse. Roblox uses Luau, which grew from Lua 5.1 but has its own syntax additions and library choices. The other source of confusion is Roblox itself: the language and the game engine aren’t the same layer.

The three examples below are small enough to read without installing anything extra. Try them in a Script in your own Studio project; the printed values described here are the results the code should produce.

Think in three layers

When you meet an unfamiliar line, sort the question first. Is it a syntax feature, a library function or an object supplied by the host? Fixing the wrong layer wastes time: changing a keyword won’t provide a missing Roblox service.

Identify the layer before looking for a fix
ExampleLayerReading question
local total = 12Basic language syntax.What value is assigned?
total += 3Luau syntax.Is the reader assuming plain Lua syntax?
table.sort(values)Library function.What input shape does the function expect?
game:GetService(…)Roblox environment.Which host supplies this object?

A plain Lua interpreter doesn’t include the Roblox data model. Likewise, a function with a familiar name in an executor may have different behavior or permissions. Check the environment as well as the spelling.

Example one: optional type annotations

local function priceAfterDiscount(price: number, discount: number): number
    return price - discount
end

local finalPrice = priceAfterDiscount(40, 7)
print(finalPrice)

This should print 33. Read the calculation first: 40 minus 7. The number annotations tell Luau’s analysis tools what kinds of arguments and return value the function expects; they don’t add another step to the arithmetic.

Type annotations are helpful while writing code, but they aren’t a general input filter. If values arrive from a player or an external service, the program still needs appropriate checks at that boundary.

Most of the example should look familiar from a Lua lesson: a local function, two parameters, a return and a call. The annotations are the new part. You don’t have to relearn all of that surrounding structure at once.

Example two: compound assignment and iteration

local deliverySizes = {4, 2, 5}
local total = 0

for _, size in ipairs(deliverySizes) do
    total += size
end

print(total)

Walk through the three values: total becomes 4, then 6, then 11. The final print should show 11. The underscore is an ordinary variable name chosen because this loop doesn’t use the index.

For this simple variable, total += size does the same update as total = total + size. Before rewriting a more complicated assignment, though, check exactly what its left-hand side refers to.

Luau also has generalized iteration. If you’re moving a snippet into a separate Lua interpreter, check which iteration forms that runtime accepts instead of assuming every familiar-looking loop will parse.

Example three: build a readable message

local playerCount = 3
local message = "Players ready: " .. tostring(playerCount)
print(message)

This should print “Players ready: 3”. The two dots join strings. Luau also supports interpolated strings, written with backticks and expressions in braces; they’re another way to assemble the message.

For interface code, don’t stop at the finished label. Follow playerCount back to where it’s assigned. A neatly formatted message can still display a stale value or a value taken from the wrong source.

Lua version numbers are not a feature checklist

Luau isn’t simply “the latest Lua.” Its compatibility reference lists the differences, including decisions related to sandboxing. Look up a feature individually when a tutorial uses another Lua version.

File-system functions are a good example. A tutorial may assume a library or host capability that your target environment doesn’t expose. Replacing a syntax error won’t resolve that missing dependency.

A practical reading sequence

  1. Mark the local variables and literal values.
  2. Identify function definitions and the calls that reach them.
  3. Underline unfamiliar syntax and check the Luau reference.
  4. Separate library calls from Roblox service or object calls.
  5. Write down environment assumptions before attempting a port.

When asking for help, keep the example small enough to expose the actual language question. “Which environment provides this function?” is easier to answer than a screenshot of a whole application followed by “Why is Lua broken?”

Primary references

Comments 0

No approved comments yet. Start the discussion.

Every comment is reviewed by a moderator before publication. Enter only a nickname and your comment; no email or account is needed.

Privacy policy