Create your own
Lesson illustration

Declaring Local Variables with Different Data Types

Hello again. Last time, you used -- comments to leave notes for people reading your Script while Roblox ignored those notes during a playtest.

Now your Script will begin to remember information. A variable gives a value a useful name, so you can use that name later rather than repeatedly writing the value itself. You will create local variables holding four basic kinds of values: numbers, strings, booleans, and nil.


A variable is a named value

Suppose a game starts every player with 25 coins. You could write 25 every time you need that number. But if you later decide the starting amount should be 50, you would have to find and replace every 25.

A variable gives that value one name:

local startingCoins = 25

Read this as: “Create a local variable named startingCoins and store the number 25 in it.”

The general pattern is:

local variableName = value

It has three parts:

PartExampleMeaning
locallocalCreates the variable as a local variable. Use this for variables you create in this course.
Variable namestartingCoinsYour meaningful label for the value.
Assignment operator and value= 25Stores the value on the right under the name on the left.

You can then use the name in print():

local startingCoins = 25

print(startingCoins)

Output shows:

25

Notice that there are no quotation marks around startingCoins inside print(). Roblox looks up the variable and prints the value stored in it.

Compare that with this:

print("startingCoins")

That prints the literal text:

startingCoins

Quotation marks mean “this is text.” Without quotation marks, Luau treats the word as a variable name.

Beginner Scripting Tutorial 2023 #3 - Local Variables

Watch “Beginner Scripting Tutorial 2023 #3 - Local Variables” by Rustysillyband for a visual demonstration of declaring a variable, assigning a value, and printing it in Roblox Studio.

Begin with string assignment to see a variable created and printed. Then watch four basic values, focusing on the difference between text, numbers, booleans, and nil. Finish with printing values to connect the declarations to Output.


Four values you will use immediately

For now, focus on these four value types.

Numbers

A number is a quantity you may eventually use for coins, health, damage, time, or a score.

local playerHealth = 100
local jumpHeight = 7.5

Both whole numbers such as 100 and decimal numbers such as 7.5 are numbers. Do not put quotation marks around a number unless you mean to store it as text.

local correctHealth = 100
local textHealth = "100"

These may look similar, but they hold different kinds of values. correctHealth holds a number; textHealth holds the characters 1, 0, and 0.

Strings

A string is text inside quotation marks. Use strings for player messages, object names, labels, and many other pieces of text.

local welcomeMessage = "Welcome to my game!"
local playerTitle = "Explorer"

The quotation marks mark where the string begins and ends. They are not part of the text that is printed.

print(welcomeMessage)

Output:

Welcome to my game!

Booleans

A boolean holds one of only two values:

true
false

Booleans are useful for game states that have a clear yes-or-no answer:

local isDoorOpen = false
local hasCollectedCoin = true

Do not write "true" or "false" when you want booleans. Those versions are strings, not boolean values.

local correctAnswer = true
local textAnswer = "true"

The first represents a true/false state. The second is only text.

nil

nil means there is no value stored here right now. It is not zero, false, or an empty string.

local selectedMap = nil

This could mean that the game has not selected a map yet.

Here are four values that beginners sometimes mix up:

ValueWhat it means
0The numeric quantity zero
falseA boolean state: false
""A string containing no characters
nilNo value is present

All four are different.

You can also create a variable without writing a value:

local selectedMap

Its value is nil. While this is valid, writing = nil is often clearer when you deliberately want to show that nothing has been selected yet.


Use clear local variable names

Variable names should communicate what the value represents. Compare:

local x = 100

with:

local playerHealth = 100

Both are valid, but playerHealth tells you what 100 means.

Use camelCase for names containing multiple words:

local startingCoins = 25
local isRoundActive = false
local welcomeMessage = "Hello!"

In camelCase, the first word begins with a lowercase letter, and each later word begins with an uppercase letter.

A variable name can contain letters, digits, and underscores, but it cannot begin with a digit. It also cannot contain spaces or hyphens.

local score2 = 10
local player_name = "Sky"

These names are valid.

-- local 2score = 10
-- local player name = "Sky"
-- local player-name = "Sky"

Those examples are not valid variable names, so they remain comments.

Also, names are case-sensitive:

local playerScore = 10
local playerscore = 20

These are two different variable names. To avoid hard-to-find mistakes, choose one spelling and use it consistently.

Variables | Documentation - Roblox Creator Hub

Read the official Roblox Creator Hub reference to reinforce variable naming, assignment, and the standard use of local.

Under “Name variables,” read case sensitivity and the surrounding naming guidance. Then, under “Assign values,” read assignment basics. In the following local-variable example, notice how each print() call uses a name but Output receives the stored value.


Build a small game-settings Script

Open your existing PracticeScript in ServerScriptService. You can replace its current print() lines with this code, keeping the comment as a reminder of the Script’s purpose:

-- Store a few starting settings for the game.

local startingCoins = 25
local welcomeMessage = "Welcome to the practice game!"
local isRoundActive = false
local selectedMap = nil

print(startingCoins)
print(welcomeMessage)
print(isRoundActive)
print(selectedMap)

Playtest the experience and inspect Output. You should see the values in the same order:

25
Welcome to the practice game!
false
nil

This Script does not yet make a game mechanic. It is a controlled test: each line proves that a local variable can hold a different kind of value and that you can retrieve the value using its name.

Read the declarations one at a time:

local startingCoins = 25

startingCoins stores a number.

local welcomeMessage = "Welcome to the practice game!"

welcomeMessage stores a string.

local isRoundActive = false

isRoundActive stores a boolean.

local selectedMap = nil

selectedMap deliberately has no value yet.

For a brief personal variation, change the values to fit a game idea you might build later. For example, change the welcome message, choose a different number of coins, and set the round state to either true or false. Keep the variable names meaningful and run the Script again to verify Output changed as expected.


A useful rule for local

For every variable you create in these early scripts, begin with local:

local damageAmount = 10
local platformName = "IcePlatform"

For now, treat local as the standard safe way to create your own variables. Later, you will study scope, which explains precisely where a local variable can be used. The immediate habit is simple: write local when you create a new variable.

One last distinction matters: the equals sign in this declaration stores a value.

local damageAmount = 10

It does not ask whether two things are equal. In the next module, you will learn the comparison operators used to test whether values match. For today, think of = as “store this value under this name.”


Key takeaways

A local variable is a meaningful name that stores a value:

local variableName = value

You declared four essential kinds of values:

  • Numbers such as 25 and 7.5
  • Strings such as "Welcome!"
  • Booleans: true or false
  • nil, meaning no value is currently present

Use descriptive, consistently spelled camelCase names such as startingCoins and isRoundActive. When you pass a variable name to print(), Roblox prints the value stored in that variable.

Next, you will inspect values more carefully with Luau’s type() function, which tells you whether a value is a number, string, boolean, nil, or another type.

Can't find a good explanation? Sign up and we'll make it for you

Sign up