Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Studio and Basics

Your First Script and the Output Window

"Write and run your first Roblox Script: add it in Explorer, use print, warn and error, change a part from code, wait with task.wait, and read errors in the Output window."

You have built a part by hand. Now you will make code do the same. In this lesson you add a Script, print your first message, change a part's color from code, and learn to read the Output window, where every message and error appears.

Adding a Script

  1. Open the Explorer and Output windows (View tab).
  2. Hover over ServerScriptService and click the + button.
  3. Choose Script. A new item named Script appears and opens in the editor.
  4. Replace the default content with the code below.
LUA
print("Hello, Roblox!")

Press Play (F5). In the Output window you will see:

TEXT
Hello, Roblox!

That is a complete, working Roblox program. print is a built-in function that writes a line to Output. The text in quotes is a string.

Which service should the script live in? A Script in ServerScriptService runs on the server when the game starts and is hidden from players. That is the right home for most game logic. A script inside a part in Workspace also runs on the server, but ServerScriptService keeps things tidy.

Comments

Comments are notes for people; Luau ignores them.

LUA
-- This is a single-line comment

--[[
	This is a
	multi-line comment
]]
print("Comments are skipped")

Good comments explain why, not what. Writing -- add 1 above x += 1 helps nobody.

Output has three levels:

LUA
print("Information")          -- normal, white text
warn("Something looks odd")   -- yellow text
error("Something is broken")  -- red text, and stops this script run

error throws an exception: the script stops at that line unless you catch it (the error handling lesson shows how). Use print while you develop, warn for problems you can survive, and error for conditions that must not happen.

You can print several values at once, separated by commas:

LUA
print("Player count:", 3, true)  --> Player count: 3 true

Changing the world from code

Create a part in Studio named Block in the Workspace, then use this script.

LUA
local block = workspace.Block

block.Color = Color3.fromRGB(0, 170, 255)
block.Material = Enum.Material.Neon
block.Anchored = true

print("The block is now", block.Color)

Read it line by line. workspace is a built-in variable for the Workspace service. workspace.Block is the child called Block. The local keyword creates a variable so we do not repeat the long path. Color3.fromRGB(0, 170, 255) makes a colour from red, green and blue amounts between 0 and 255. Enum.Material.Neon is a named choice from a fixed list.

Press Play and the block turns glowing blue. If you stop the test, the part goes back, because the test ran on a copy of the place, as you saw in the first lesson of this chapter.

Waiting

Scripts run top to bottom and finish. To pause, use task.wait, which suspends only this script for the given number of seconds:

LUA
local block = workspace.Block

for i = 1, 5 do
	block.Color = Color3.fromRGB(255, 0, 0)
	task.wait(0.5)
	block.Color = Color3.fromRGB(0, 0, 255)
	task.wait(0.5)
end

print("Done flashing")

The block flashes red and blue five times. task.wait() with no number waits one frame (about 1/60 of a second). Never write a while true do loop without a task.wait() inside; without it the loop would freeze the whole server until Roblox stops the script.

Reading Output

The Output window shows each message with a timestamp and the script name and line that produced it. Clicking a line number in a red error jumps to the problem.

A typical mistake looks like this.

LUA
local block = workspace.Blok   -- typo: the part is called Block
block.Color = Color3.fromRGB(255, 0, 0)

Output:

TEXT
Blok is not a valid member of Workspace "Workspace"
Stack Begin
Script 'ServerScriptService.Script', Line 1
Stack End

The message names the missing member, the script and the line. A yellow underline in the editor often warns you before you run.

Common error messages:

Message Usual cause
X is not a valid member of Y Typo, or the child does not exist yet
attempt to index nil with 'Y' A variable you thought was an object is nil
attempt to call a nil value Misspelt function name
Infinite yield possible on ... WaitForChild waiting for something that never arrives

Stand-in: an Output window

Real Roblox prints these for you. The JavaScript stand-in below imitates the Output panel so you can practise reading it: it prints, warns, throws an error that stops the script, and shows the line.

JS Stand-in: print, warn and error (JavaScript)

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

No output.

The last print never runs because the error stopped the script, just like in Studio.

Stand-in: task.wait and a flashing loop

task.wait suspends one script without freezing others. JavaScript models this with async/await. Two "scripts" run at once, each waiting its own time:

JS Stand-in: scripts that wait independently (JavaScript)

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

No output.

The lines interleave because each script waits independently. In Roblox each Script runs on its own, and task.wait is what makes the interleaving work.

Good habits from day one

  • Give every script and part a clear name. Script and Part tell you nothing after the tenth one.
  • Put server code in ServerScriptService.
  • Print often while learning; delete noisy prints before publishing.
  • Fix the first error first. Later errors are often side effects of the first.
  • Use the Command bar for one-off experiments.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does the text from print appear?

    print writes to Output, which you open from the View tab.
  2. 2. What does a loop that never waits do?

    A busy loop without task.wait never yields, so the engine eventually stops it. Always wait inside while true loops.
  3. 3. How do you write a single-line comment in Luau?

    Luau uses two dashes for single-line comments and --[[ ]] for multi-line.
  4. 4. What happens after error("x") runs in a script?

    error throws an exception that stops the current run unless it is caught.

Summary

  • Add a Script to ServerScriptService; Play shows its output in the Output window.
  • print, warn and error write normal, yellow and red messages; error stops the run.
  • workspace.Block.Color = ... changes the world; task.wait(seconds) pauses one script.
  • Read errors for the script name and line; fix the first one first.

Next: Services and Containers