How to Script on Roblox: Your First Luau Script
By Blox Handbook•
You script on Roblox by adding a Script object in Roblox Studio and writing code in Luau, Roblox’s programming language. Scripts make things happen: doors that open, points that add up, parts that react when a player touches them.
The 3 types of scripts
Roblox has 3 kinds of script, and choosing the right one matters:
| Script type | Where it runs | Use it for |
|---|---|---|
| Script | On the server | Game rules, saving data, anything players must not be able to cheat |
| LocalScript | On each player’s device | Camera, user interface, input from the keyboard or touch screen |
| ModuleScript | Wherever it is required | Code you want to share between other scripts |
How to add your first script
- Open Roblox Studio with a Baseplate.
- Insert a Part from the Home tab and set Anchored to true.
- In the Explorer, right-click the part and choose Insert Object > Script.
- Open View > Output so you can see messages from your code.
Your first script, line by line
Replace the default code with:
local part = script.Parent
local touches = 0
part.Touched:Connect(function(hit)
touches += 1
print(hit.Name .. " touched the part. Total touches: " .. touches)
end)Here is what each part does:
script.Parentis the object the script sits inside, here the part.local touches = 0creates a variable that stores a number.part.Touched:Connect(...)runs the function every time something touches the part.touches += 1adds one to the counter.print(...)writes a message to the Output window.
Press Play, walk onto the part, and watch the Output window count your touches.
What to learn next
After your first script, learn these 4 topics in order:
- Properties: changing size, color and position from code.
- Events: responding to players joining, touching and clicking.
- Functions and tables: organizing your code and data.
- Server and client: when to use a Script and when a LocalScript.
The language itself is explained in what coding language Roblox uses, and Roblox Studio’s built-in AI helper is covered in Roblox Studio AI.