---
url: /programming-fundamentals/variables-and-values.md
description: Store values in variables, name them well, and trace what each line does.
---

# Variables and Values

*Written: {{ $frontmatter.date }}*

## Learning objectives

* You explain what a variable is in your own words.
* You create [variables](https://create.roblox.com/docs/luau/variables) with `local` and give them values with `=`.
* You name variables with camelCase and full words.
* You tell values apart from the variables that hold them.

## Prerequisites

* Read [First Script](../getting-started/first-script.md). You already ran code and pressed Play.

## What a variable is

A variable is a name you give to a value so you can use it later. Think of it as a label on a box. The label is the variable. The thing inside the box is the value.

Values are the actual data. `"Maya"` is a value. `10` is a value. `true` is a value. A variable is the name you stick on one of those values so your code can find it again.

Run this: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
local playerName = "Maya"
print(playerName)
```

```txt
Maya
```

Walkthrough:

1. Line 1 turns on strict mode. Every script in this course starts with it.
2. Line 2 creates a variable called `playerName` and puts the value `"Maya"` inside it.
3. Line 3 prints whatever `playerName` currently holds.

## Create variables with local

You create a variable in two steps. Write `local`, then the name, then `=`, then the value. The `=` sign is called assignment. It copies the value on the right into the variable on the left.

The word `local` matters. It keeps the variable inside your script. Without it, the variable leaks into a shared global place and runs slower. Always write `local`. You can read more about why in [scope](https://create.roblox.com/docs/luau/scope).

Run this: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
local score = 0
print(score)
score = 10
print(score)
```

```txt
0
10
```

Walkthrough:

1. Line 1 turns on strict mode.
2. Line 2 creates `score` and assigns it `0`.
3. Line 3 prints `0`, the current value of `score`.
4. Line 4 assigns a new value, `10`, to the same variable. The old `0` is gone.
5. Line 5 prints `10`.

Notice that line 4 has no `local`. You use `local` once, when you create the variable. After that you assign with a plain `=`.

You can also create a variable with no value yet. It then holds a special value called nil, which means "nothing here". You will meet nil properly in [Strings, Numbers, and Nil](strings-numbers-nil.md).

Run this: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
local nickname = "Max"
local teamScore = 4
local finished = false
print(nickname)
print(teamScore)
print(finished)
```

```txt
Max
4
false
```

Walkthrough:

1. Line 1 turns on strict mode.
2. Line 2 stores the [strings](https://create.roblox.com/docs/luau/strings) value `"Max"` in `nickname`.
3. Line 3 stores the [numbers](https://create.roblox.com/docs/luau/numbers) value `4` in `teamScore`.
4. Line 4 stores the [booleans](https://create.roblox.com/docs/luau/booleans) value `false` in `finished`.
5. Lines 5 to 7 print each variable in order.

## Names that help you

Good names make your code read like sentences. Follow three rules.

First, use camelCase. Start with a lowercase letter. Capitalize the first letter of each extra word. No spaces, no dashes.

Second, spell words out fully. `teamScore` beats `ts`. `playerName` beats `pn`. You read code far more often than you type it.

Third, say what the thing means, not what type it is. `winnerName` beats `string1`.

Run this: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
local winnerName = "Maya"
local runnerUpName = "Rio"
print(winnerName)
print(runnerUpName)
```

```txt
Maya
Rio
```

Walkthrough:

1. Line 1 turns on strict mode.
2. Line 2 creates `winnerName`. Each word after the first starts uppercase.
3. Line 3 creates `runnerUpName` with the same style.
4. Lines 4 and 5 print both values.

Names must start with a letter or an underscore. They can contain letters, digits, and underscores after that. They cannot be reserved words like `if`, `then`, `local`, or `true`. Names are case-sensitive, so `score` and `Score` are two different variables.

## Values live on their own

A variable only points at a value, which exists on its own. Two variables can point at the same value. Changing one variable never changes the other.

Run this: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
local firstPick = "apple"
local secondPick = firstPick
print(firstPick)
print(secondPick)
firstPick = "pear"
print(firstPick)
print(secondPick)
```

```txt
apple
apple
pear
apple
```

Walkthrough:

1. Line 1 turns on strict mode.
2. Line 2 puts the value `"apple"` in `firstPick`.
3. Line 3 copies the current value of `firstPick` into a new variable called `secondPick`. Now both hold `"apple"`.
4. Lines 4 and 5 print `apple` twice.
5. Line 6 gives `firstPick` a new value, `"pear"`. This touches only `firstPick`.
6. Line 7 prints `pear` for `firstPick`.
7. Line 8 prints `apple` for `secondPick`. It kept its own copy.

This split causes many beginner bugs. When output looks wrong, ask yourself: which variable did I actually change, and which one did I actually print?

## Common mistakes

**Forgetting `local`.**

Run this to see the sloppy version: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
score = 5
print(score)
```

```txt
5
```

This prints `5`, but `score` becomes a global variable. Strict mode underlines it, and globals run slower. Fix it by adding `local` where you create the variable. Run the fix the same way.

```luau
--!strict
local score = 5
print(score)
```

```txt
5
```

**Putting spaces or dashes in names.**

Run this to see it fail: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
local team score = 4
print(team score)
```

```txt
Red syntax error on line 2: the script does not run until you fix the name.
```

Luau reads `team` and `score` as two separate names. Fix it with camelCase. Run the fix the same way.

```luau
--!strict
local teamScore = 4
print(teamScore)
```

```txt
4
```

**Mixing up the value and the variable.**

Run this to see it fail: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
local playerName = Maya
print(playerName)
```

```txt
nil
```

`Maya` with no quotes is read as a variable name. That variable does not exist, so strict mode flags it and it arrives as `nil`. Fix it by quoting text values. Run the fix the same way.

```luau
--!strict
local playerName = "Maya"
print(playerName)
```

```txt
Maya
```

**Reusing `local` on every assignment.**

Run this: put it in a Script in ServerScriptService and press Play.

```luau
--!strict
local score = 0
local score = 10
print(score)
```

```txt
10
```

This prints `10`, but the second `local` builds a brand-new variable that hides the first one. Two variables with one name confuse every reader. Fix it by assigning without `local` after creation. Run the fix the same way.

```luau
--!strict
local score = 0
score = 10
print(score)
```

```txt
10
```

## Exercises

Run every solution below in a Script in ServerScriptService and press Play. Try each exercise yourself before you open its solution.

1. Create a variable called `petName` with your favorite animal name in it. Print it.

::: details Solution 1

```luau
--!strict
local petName = "Biscuit"
print(petName)
```

```txt
Biscuit
```

:::

2. Create `coins` with the value `20`. Print it. Then change `coins` to `35` and print it again.

::: details Solution 2

```luau
--!strict
local coins = 20
print(coins)
coins = 35
print(coins)
```

```txt
20
35
```

:::

3. Create `firstName` and `lastName` with two words in them. Print each one on its own line.

::: details Solution 3

```luau
--!strict
local firstName = "Maya"
local lastName = "Reyes"
print(firstName)
print(lastName)
```

```txt
Maya
Reyes
```

:::

4. Predict the output before you run it. Then run it and check yourself.

::: details Solution 4

```luau
--!strict
local greeting = "hi"
local copiedGreeting = greeting
greeting = "bye"
print(greeting)
print(copiedGreeting)
```

```txt
bye
hi
```

The third line copies `"hi"` into `copiedGreeting`. The fourth line changes only `greeting`, so `copiedGreeting` still prints `hi`.

:::

## Summary

You learned that a variable is a named label for a value. You create variables with `local`, assign values with `=`, and name them in camelCase with full words. Values stand on their own, and each variable holds its own copy.

Next: [Operators and Logic](operators-and-logic.md).
