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 typeWhere it runsUse it for
ScriptOn the serverGame rules, saving data, anything players must not be able to cheat
LocalScriptOn each player’s deviceCamera, user interface, input from the keyboard or touch screen
ModuleScriptWherever it is requiredCode you want to share between other scripts

How to add your first script

  1. Open Roblox Studio with a Baseplate.
  2. Insert a Part from the Home tab and set Anchored to true.
  3. In the Explorer, right-click the part and choose Insert Object > Script.
  4. 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.Parent is the object the script sits inside, here the part.
  • local touches = 0 creates a variable that stores a number.
  • part.Touched:Connect(...) runs the function every time something touches the part.
  • touches += 1 adds 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:

  1. Properties: changing size, color and position from code.
  2. Events: responding to players joining, touching and clicking.
  3. Functions and tables: organizing your code and data.
  4. 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.