Game Creation
Luau Scripting Basics: Variables, Functions and Events
Luau is the programming language used for scripts in Roblox Studio. A script is a set of written instructions that tells your game what to do: open a door, give a player a point, start a countdown. Without scripts a place is a still picture that players can walk around. With them it becomes a game that reacts.
You do not need previous coding experience to follow this guide. It explains three building blocks that appear in nearly every script, variables, functions and events, using small examples you can type and change yourself. If you are still setting up Studio, read a beginner guide to the tool first, then come back.
What a script actually is
A script is plain text, read one line after another, starting at the first. The computer does exactly what each line says, in order, and it does not guess what you meant. That strictness is why small mistakes, like a missing bracket or a misspelled name, cause errors.
Studio lets you add a script to an object in your place. Where you put it matters, because the documentation describes different script types that run in different places, some on the server that runs the game and some on each player's device. Beginners usually start with a server script, which controls shared things such as a door. The creator documentation explains the current script types and where each belongs, and it is worth reading before you build anything large.
A line that begins with two dashes is a comment. The computer ignores comments, so they are the place for notes to your future self. Write them often. A script you understand today can look like a puzzle in a month.
Variables: names for things you want to remember
A variable is a name attached to a value. You create one with the word local, then a name, then an equals sign, then the value. For example, a variable called score could hold the number zero, and a variable called message could hold some text in quotation marks.
Values come in a few basic kinds:
- Numbers, used for scores, speeds and counts.
- Strings, which are pieces of text placed in quotation marks.
- Booleans, which are only ever true or false, useful for questions like whether a door is open.
- Objects from your place, such as a part, which you can store in a variable so you can change its color or position.
Choose names that describe the contents. A variable called doorIsOpen explains itself, while one called x forces you to remember. Luau cares about capital letters, so Score and score are two different names, and mixing them up is one of the most common beginner errors.
Variables can change. Setting score to its old value plus one is how a game counts. This is the same idea as keeping score on paper, except the script never forgets to update it.
Functions: reusable steps with a name
A function is a named group of instructions that you can run whenever you want. Instead of writing the same five lines in ten places, you write them once inside a function and call it by name. If you later need to change what happens, you change it in one spot.
A function can also receive information and give back a result. A function that receives a number and returns that number doubled is a small example. The information you hand in is called a parameter, and what comes back is the return value. This is how larger scripts stay organized: each function does one clear job, and the main script connects them.
When you write functions, aim for these habits:
- Give each function one job and a name that states it, such as openDoor or addPoint.
- Keep functions short enough to read in one glance.
- Write a comment above any function whose purpose is not obvious from its name.
- Test each function on its own before connecting it to others.
Defining a function does not run it. Many beginners write a function, test the game, and wonder why nothing happened. The function exists, but nothing has called it yet.
Events: letting the game tell you when something happens
Events are what turn a script from a list of instructions into something interactive. An event is a signal that an object sends when something takes place: a player touches a part, a player joins the game, a button is pressed. Your script can listen for the signal and run a function in response, which the documentation calls connecting a function to an event.
The classic first project is a part that changes color when a player touches it. The script stores the part in a variable, writes a function that changes the part's color, and connects that function to the part's touch event. After that, you never call the function yourself. The game calls it each time the event fires.
A few cautions apply once you start using events:
- The touch event can fire many times in a short span, because a player's body touches the part with several pieces. Add a simple check, such as a boolean that blocks repeated runs for a moment.
- Events belong to specific objects, and different objects offer different events. The documentation lists what each object type provides.
- Information about who or what triggered the event is passed to your function as a parameter. Read it, and check that it is what you expect before using it.
Finding and fixing errors
Every programmer sees errors daily, so treat them as information rather than failure. Studio has an output window that prints error messages and anything you ask your script to print. A message usually names the script and the line where the problem was noticed. Start there.
When something does not work, go through a short routine:
- Read the whole error message slowly, including the line number.
- Check spelling and capital letters in every name on that line and the lines just above it.
- Print the value of a variable to see whether it holds what you think it holds.
- Undo your last change and see whether the problem disappears.
- Look the feature up in the creator documentation.
Be careful with scripts you find online. Models from other people and copied code can contain hidden scripts that do things you did not ask for, including ones that break the rules of the platform or damage your game. Read every line of anything you add, delete what you do not understand, and never paste code from someone who says it will give you Robux or access to an account, since no code outside Roblox can add Robux and such offers are scams.
Practice plan for the next few sessions
Skill comes from small exercises repeated with variations. Work through these in order, and ask a parent or teacher to look at your progress now and then, since explaining your code aloud exposes mistakes quickly.
- Write a script that prints a message when the game starts, to confirm you can add and run a script.
- Store a number in a variable, change it, and print it before and after.
- Write a function that changes a part's color, and call it once from the script.
- Connect that function to a touch event, then add a check that prevents repeated triggering.
- Add a score variable that goes up by one on each valid touch and prints the new total.
- Read the scripting section of the creator documentation and rewrite one of your scripts with clearer names and comments.
Learning to script is gradual, and most of your early time will go to fixing small mistakes. That is normal, and each fix teaches you something you will use again.
Informational content only. Big Money Plan is an independent publication and is not affiliated with Roblox Corporation or any other game company. We do not sell or give away Robux, codes, gift cards or items, and nothing here is a promise of any reward. Platforms change their features, prices and rules: confirm current details on the platform's own pages. If you are under 18, involve a parent or guardian before any purchase.