Scripting
Scripts are plain text files that let you add custom, dynamic behavior to your
experiences. You can use scripts to trigger in-game events, respond to player
input, save player data, create leaderboards, spawn enemies, control NPC behavior,
and much, much more.
This section is for creators with some coding experience who want to know the
specifics of scripting in Roblox.
If you've never written code before and want an introduction to programming, see
Coding fundamentals, which covers concepts like variables, functions, conditionals,
loops, and arrays. For a more guided, step-by-step approach, see the Basic gameplay
tutorial.
Luau
Roblox scripts use the Luau programming language, which is derived from Lua 5.1.
Compared to Lua 5.1, Luau adds performance enhancements and many useful features,
including an optional typing system, string interpolation, and generalized
iteration for tables.
All valid Lua 5.1 code is valid Luau code, but the opposite is not true.
Most books and online resources for Lua are still broadly applicable to Luau. For a
detailed summary of differences, see Compatibility in the Luau documentation. For
language syntax, see the Luau reference.
Luau basics
Luau is gradually typed, so you don't need to specify a type when you create a
variable. You can use type() to check object type:
logMessage = "User has more than 10 items!"
print(logMessage) --> User has more than 10 items!
print(type(logMessage)) --> string
Luau has global and local scopes, but it's almost always better to declare
variables and functions locally with the local keyword:
local logMessage = "User has more than 10 items!"
local function printMessage()
print(logMessage)
end
printMessage() --> User has more than 10 items!
Luau uses nil to represent nonexistence or nothingness, which evaluates as false in
conditional statements:
local messageToUser
print(messageToUser) --> nil
print(type(message)) --> nil
if messageToUser then
-- statement evaluates to false
end
As you might have noticed, -- starts a one-line comment. --[[]] creates a block
comment:
--[[
Shuts off the cosmic moon ray immediately.
Should only be called within 15 minutes of midnight Mountain Standard
Time to avoid damage to the cosmic moon ray.
]]
local function stopCosmicMoonRay()
-- add this later, it might prove important
end
Tables are the generic term for arrays and dictionaries. Arrays are one-based
rather than zero-based, so the first item is [1]. You declare arrays and
dictionaries with a single set of curly braces:
local myArray = {"chips", "sparkling water", "salsa"}
local myDictionary = {
snack = "chips",
drink = "sparkling water",
dip = "salsa"
}
print(myArray[1]) --> chips
print([Link]) --> salsa
You can iterate over tables using for loops with the ipairs() function for arrays
and the pairs() function for dictionaries, but Luau also lets you omit these
functions for cleaner syntax:
for index, value in ipairs(myArray) do -- standard Lua
print(index, value)
end
for key, value in pairs(myDictionary) do -- standard Lua
print(key, value)
end
for key, value in myDictionary do -- Luau generalized iteration
print(key, value)
end
Your first script
In Roblox Studio, hover over ServerScriptService in the Explorer window and click
+.
Select Script to add a new script.
Right-click the script and rename it to HelloScript.
Double-click the script to open it in the Script Editor.
Add the following code to the file:
local helloArray = {"h", "e", "l", "l", "o"}
local worldArray = {"w", "o", "r", "l", "d"}
for index, value in helloArray do
print(value)
end
print([Link](worldArray))
Ensure that the Output window is open.
Initiate a playtest to run your experience and note the output:
h
e
l (x2)
o
world