Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 37 ยท Luau Scripting

Strings and Operators in Luau

"Work with Luau strings and operators: quotes, concatenation, backtick interpolation, string functions, arithmetic, comparison, logical operators, and the length operator."

Text and arithmetic show up in every Roblox feature: player names, chat messages, scoreboards, damage numbers. This lesson covers how to write strings, join them, use Luau's handy interpolation syntax, and the operators for math, comparison and logic.

Writing strings

A string can use double quotes, single quotes, or backticks:

LUA
local a = "Hello"
local b = 'World'
local c = `Hello World`

print(a, b, c)
print("She said \"hi\"")   --> She said "hi"
print('It\'s fine')        --> It's fine
print("Line one\nLine two")
print("Tab\there")

Common escapes: \n newline, \t tab, \\ backslash, \" and \' quotes. For long text that spans lines, use long brackets: [[ ... ]]. Strings are immutable: functions return new strings; they never change the original.

Joining strings

The .. operator concatenates:

LUA
local first = "Ada"
local last = "Lovelace"
print(first .. " " .. last)      --> Ada Lovelace
print("Score: " .. 120)          --> Score: 120   (numbers convert automatically)

Beware: .. binds tighter than comparison operators, and joining a nil errors (attempt to concatenate nil).

String interpolation

Luau's best string feature is interpolation: a backtick string with inside. It is shorter and clearer than ...

LUA
local name = "Mira"
local coins = 12

print(`{name} has {coins} coins`)                --> Mira has 12 coins
print(`Double: {coins * 2}`)                     --> Double: 24
print(`Rounded: {math.floor(7.9)}`)              --> Rounded: 7
print(`Name length: {#name}`)                    --> Name length: 4

Any expression is allowed inside the braces. To print a literal brace, escape it with a backslash: \{.

Useful string methods

Strings have methods you call with a colon (:):

LUA
local text = "Roblox Studio"

print(#text)                    --> 13  (the # operator gives the length)
print(text:upper())             --> ROBLOX STUDIO
print(text:lower())             --> roblox studio
print(text:sub(1, 6))           --> Roblox  (characters 1 to 6)
print(text:find("Studio"))      --> 8  13
print(text:gsub("o", "0"))      --> R0bl0x Studi0  2
print(("ab"):rep(3))            --> ababab
print(string.format("%.2f", 3.14159))   --> 3.14
print(string.format("%03d", 7))         --> 007

Positions start at 1, not 0. This is true for strings and tables in Lua and Luau.

More you will use: string.split("a,b,c", ",") (returns a table), text:len(), text:byte(), string.char(65). For checking text from players (chat messages, names), use string.lower before comparing so "HELLO" and "hello" match.

Luau strings are sequences of bytes. For text with accents or emoji, use the utf8 library (utf8.len, utf8.char) when you count characters.

Arithmetic operators

Operator Meaning Example Result
+ add 7 + 2 9
- subtract 7 - 2 5
* multiply 7 * 2 14
/ divide (always a decimal) 7 / 2 3.5
// floor divide 7 // 2 3
% remainder 7 % 2 1
^ power 2 ^ 3 8
-x negate -5 -5

Order of operations follows normal math: ^ first, then * / // %, then + -. Parentheses override it.

% is excellent for cycling: index % 4 always returns 0 to 3, and seconds % 60 gives the seconds part of a clock.

Comparison operators

They produce a boolean. Note that not equal is ~=, not !=.

LUA
print(5 == 5)   --> true
print(5 ~= 6)   --> true
print(5 > 6)    --> false
print(5 <= 5)   --> true
print("a" < "b")  --> true  (alphabetical)

Comparing different types with == is just false (1 == "1" is false); but < between a number and a string errors.

Logical operators: and, or, not

LUA
print(true and false)   --> false
print(true or false)    --> true
print(not true)         --> false

and and or return one of their operands, not just true or false. This makes a neat idiom for defaults:

LUA
local speed = settings.speed or 16      -- use 16 if speed is nil
local label = player and player.Name or "Guest"

and stops at the first false value; or stops at the first true value (short-circuiting). Luau also has an if expression for choosing a value:

LUA
local status = if health > 0 then "alive" else "dead"

Precedence cheat sheet

From tightest to loosest: ^, unary not - #, * / // %, + -, .., comparisons, and, or. When unsure, add parentheses; it costs nothing and makes code readable.

Stand-in: interpolation

The next two stand-ins imitate Luau behaviour in JavaScript. First, interpolation and string helpers, including 1-based positions.

JS Stand-in: string tools with Luau's 1-based positions (JavaScript)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stand-in: operators and short-circuiting

JS Stand-in: // % and the or default idiom (JavaScript)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice 0 or 16 stays 0: a number of zero is a legitimate value in Luau, and that matters for settings like volume or 1.

Common mistakes

  • Using != instead of ~=, or && instead of and.
  • Counting from 0 in strings and tables.
  • Forgetting .. has lower priority than +: "a" .. 1 + 2 works as "a" .. 3.
  • Concatenating nil; use interpolation with a fallback, written as {value or "none"} inside a backtick string.
  • Comparing floating-point numbers for exact equality.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which line prints Mira has 12 coins when name is Mira and coins is 12?

    Interpolation needs backticks. Plain quotes print the braces literally.
  2. 2. What does 7 // 2 evaluate to?

    // is floor division: 7 divided by 2 is 3.5, rounded down to 3.
  3. 3. What is the not-equal operator in Luau?

    Luau writes not equal as ~=.
  4. 4. What does #"Roblox" return?

    gives the length of a string; Roblox has six characters.

Summary

  • Strings use "...", '...' or backticks; .. joins; backtick interpolation is the clearest way to build messages.
  • String methods (upper, sub, find, gsub, format, split) count from 1 and return new strings.
  • Arithmetic has //, % and ^; comparison uses ~=; logic uses and, or, not with short-circuiting.
  • x or default and if ... then ... else expressions pick values compactly.

Next: Control Flow: Decisions and Loops