Learn EXACTLY When and How to Use “If Statements”

·

·

Stop Guessing: Learn Exactly How “If Statements” Work in Roblox Lua

If you’re new to scripting in Roblox Studio, you’re probably staring at code and wondering:
“How does this thing know when to run something?”

The answer? It’s all about logic. More specifically—if statements.

In this post, you’ll get a clear, no-fluff walkthrough of how to use if, else, and elseif in Lua. These are the basic tools that help your game make decisions.

Let’s break it all down in plain English.

Why If Statements Matter in Roblox

Picture this:

A player touches a part.
You want the part to disappear only if they have a certain item.
Or maybe they need to be a certain level before entering a door.

This kind of logic is everywhere in games.
And it’s made possible with if statements.

They’re how your script says:
“If this condition is true, then do this. Otherwise, do something else.”

First, Set Up a Clean Script

Before we dive in, start fresh:

  1. Open Roblox Studio.
  2. Insert a new Script into the Workspace.
  3. Rename it to IfStatements so you stay organized.

Done? Cool. Now let’s start coding.

Writing Your First If Statement in Lua

Here’s the most basic version of an if statement:

Lua
if 2 + 2 == 4 then
	print("2 + 2 does equal 4")
end

Let’s break that down:

  • if starts the logic check.
  • 2 + 2 == 4 is the condition.
  • == compares two values (not the same as = which assigns).
  • then tells Lua what to do if the condition is true.
  • end closes the block.

Run the script and you’ll see 2 + 2 does equal 4 in the Output window.

What Happens When the Condition Is False?

Change the math slightly:

Lua
if 2 + 3 == 4 then
	print("This won’t run")
end

Since 2 + 3 equals 5, the condition is false.
Nothing prints. The code skips over the print line.

Adding an Else Statement

If the condition fails, you can tell Lua to do something else.

Lua
if 2 + 3 == 4 then
	print("2 + 3 equals 4")
else
	print("The if statement failed")
end

Now you’ll see The if statement failed because the first condition wasn’t true.

Keep your else lined up directly under your if. Lua depends on this structure.

When One Condition Isn’t Enough — Use ElseIf

You can check multiple possibilities with elseif:

Lua
if 2 + 3 == 4 then
	print("2 + 3 equals 4")
elseif 2 + 2 == 4 then
	print("2 + 2 does actually equal 4")
else
	print("None of these are true")
end

Here’s what happens:

  • The first check fails.
  • The second one passes.
  • The script prints the second message and skips the else.

You can add as many elseif lines as you want. Lua will check them in order.

A Real Example Using Functions

Let’s wrap the logic into a reusable function:

Lua
local function addNumbers(num1, num2)
	local result = num1 + num2

	if result == 4 then
		print("The result is equal to 4")
	elseif result == 6 then
		print("The result is equal to 6")
	else
		print("None of these results add up")
	end
end

addNumbers(2, 2)
addNumbers(3, 3)
addNumbers(10, 10)

Run the code and you’ll see messages for each case.

This is a clean, organized way to handle logic without repeating yourself.

Bonus: Two If Statements vs. ElseIf

Here’s a quick trick question. What happens with this?

Lua
if result == 4 then
	print("Result is 4")
end

if result == 4 then
	print("Result is still 4")
end

Both print statements will run—because these are separate if blocks.
But if you use if, elseif, and else, only one block will run.

This can make a big difference in how your scripts behave.

What You Should Do Next

Open Roblox Studio and try it for yourself.
Start with basic math examples, then test with your own functions.

See what happens when you change the conditions.
Try nesting if statements. Test what happens when everything fails.

Once you get the hang of it, you’ll start thinking in logic naturally—just like a real developer.

Drop your test code in the comments. Let others learn from your experiments.

Now you know how your script “thinks.” That’s a big deal.

You’re ready for the next step.