Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Mastering Roblox Coding
  • Toc
  • feedback
Mastering Roblox Coding

Mastering Roblox Coding

By : Mark Kiepe
4.8 (5)
close
Mastering Roblox Coding

Mastering Roblox Coding

4.8 (5)
By: Mark Kiepe

Overview of this book

Roblox is a game platform with over 47 million daily active users. Something unique to Roblox is that you’re playing games made by other gamers! This means that you can make your own games, even if you have no experience. In addition, Roblox provides a free engine that allows you to create and publish a simple game in less than five minutes and get paid while at it. Most Roblox games require programming. This book starts with the basics of programming in Roblox Luau. Each chapter builds on the previous one, which eventually results in you mastering programming concepts in Lua. Next, the book teaches you complex technologies that you can implement in your game. Each concept is explained clearly and uses simple examples that show you how the technology is being used. This book contains additional exercises for you to experiment with the concepts you’ve learned. Using best practices, you will understand how to write and build complex systems such as databases, user input controls, and all device user interfaces. In addition, you will learn how to build an entire game from scratch. By the end of this book, you will be able to program complex systems in Roblox from the ground up by learning how to write code using Luau and create optimized code.
Table of Contents (16 chapters)
close
1
Part 1: Start Programming with Roblox
5
Part 2: Programming Advanced Systems
12
Part 3: Creating Your Own Simulator Game

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.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete