Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Roblox Coding

You're reading from   Mastering Roblox Coding The unofficial guide to leveling up your Roblox scripting skills and building games using Luau programming

Arrow left icon
Product type Paperback
Published in Aug 2022
Publisher Packt
ISBN-13 9781801814041
Length 424 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mark Kiepe Mark Kiepe
Author Profile Icon Mark Kiepe
Mark Kiepe
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Part 1: Start Programming with Roblox
2. Chapter 1: Getting Up to Speed with Roblox and Luau Basics FREE CHAPTER 3. Chapter 2: Writing Better Code 4. Chapter 3: Event-Based Programming 5. Part 2: Programming Advanced Systems
6. Chapter 4: Securing Your Game 7. Chapter 5: Optimizing Your Game 8. Chapter 6: Creating User Interfaces for All Devices 9. Chapter 7: Listening to User Input 10. Chapter 8: Building Data Stores 11. Chapter 9: Monetizing Your Game 12. Part 3: Creating Your Own Simulator Game
13. Chapter 10: Creating Your Own Simulator Game 14. Index 15. Other Books You May Enjoy

Understanding scopes

In this section, we will learn what scopes are and why you have to keep them in mind when coding. First, we will start by looking at how a new scope is made. Scopes are something you have already worked with; you just did not realize it. Every time we had an if statement, did you notice how the code before the end was spaced to the right? The reason for this spacing is because this section is a new scope. A quick note: spacing your code to the right does not create a new scope.

But what can we do with scopes? Scopes are not necessarily something that you can use but rather something you have to keep in mind. We have already seen a new scope being created when using an if statement. There are a lot more situations where scopes are created. The following code is something you can do for the sole purpose of creating a scope:

do
    print("New Scope")
end

When a scope starts, it executes the code within it. If there is another scope within an already existing scope, all of the data from the scope above is for the scope below. If there is data in a scope that the current scope is not in, you cannot access that data.

Scopes accessing certain data sounds really confusing. Here is an example that demonstrates it:

local outsideScopeData = "this is accessible everywhere"
do
    -- New Scope
    print(outsideScopeData)
    -- New data in this scope
    local insideScopeData = "This data is accessible in 
    this scope."
    -- Printing in scope data
    print(insideScopeData)
end
-- Printing data outside of the scope
print(outsideScopeData)
print(insideScopeData) – This data does not exist

Our first variable is not inside of a scope. Therefore, the scope it is in is called the global scope. This section of your code is accessible everywhere in your code.

Next, we start our first scope. To test whether we can access the variable from the global scope, we have a print statement that prints the outsideScopeData variable. We can access this variable because your new scope is inside the global scope. Because our new scope is inside the global scope, all of the data that the global scope has, our new scope has as well.

Then, we create a new variable inside of our new scope. This data does not exist in the global scope. Inside our new scope, we print this new data using the print() function. We can do this because this data is inside the current scope and thus exists.

Our scope comes to an end. We can see this because of the end line. After this, we are back in the global scope. First, we try to print outsideScopeData. We can do this because this variable exists in the global scope. Then, we try to print a variable created inside a scope. However, printing this variable does not work. Printing this does not work because our current scope, the global scope, does not exist inside the scope where this variable has been made. Therefore, we cannot access the data from here.

What if we wanted to make the variable inside the scope and still print it inside the global scope? We can make a variable with no data inside the global scope. Then, we can set this empty variable to our desired value inside the new scope. This works because the variable is made in the global scope and set inside another scope. You can see this in practice in the following code snippet:

local outsideScopeData = "this is accessible everywhere"
local dataSetInScope
do
    -- New Scope
    print(outsideScopeData)
    -- Setting data in this scope
    dataSetInScope = "This data is accessible in this 
    scope."
    -- Printing in scope data
    print(dataSetInScope)
end
-- Printing data outside of the scope
print(outsideScopeData)
print(dataSetInScope)

As you can see, we have created a variable missing the = your_data part of a variable. If we do this, Luau automatically turns this into a nil variable. We could have done this ourselves as well. Our variable would have looked like this:

local dataSetInScope = nil

Then, inside the scope, we set the variable with new data. Because the variable exists in the global scope, the data is not thrown away when the scope ends. This is why we can print the correct result in the last line of this script.

Now that we know how scopes work, we have finished everything that the first chapter offers. In the following section, we put into practice the learned information.

You have been reading a chapter from
Mastering Roblox Coding
Published in: Aug 2022
Publisher: Packt
ISBN-13: 9781801814041
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image