Roblox Scripting Beginner Guide: Variables

Variables
In simple terms, a variable is something that can store something. In malls, you probably use a shopping cart to carry items and why do you do that? Because it is a much more efficient and easy to carry multiple items rather than individually carrying each of them. Variables serve the same purpose. It stores a value, making it easier to access later in your game.
Eg:

--Ignore the local, we'll come to that later.
local item = "Bread"
local friend = "Jack"
local school = "Roblox University"

Here you can see that item is used to reference Bread, friend is used to reference Jack and so on.

Why Use Variables

Why not just type Jack or Bread?
Variables make it easy to reference values, so instead of typing out a loooong name again and again, you can assign it to a variable:

local longName = "pneumonoultramicroscopicsilicovolcanoconiosis" --hmm thts a pretty looong word!

--With variable
print(longName) -- Very clean and easy to read and understand

--Without variable
print("pneumonoultramicroscopicsilicovolcanoconiosis") --the type of code that will drive you crazy

Different Types Of Variables

Now you might have noticed that we put Jack under double quotes(this " " thingy). That’s because in programming, there are different types of variables, just like how you would classify Milk as Dairy or Eggs as Poultry. You can’t go and put a chicken in the veggies section and call it lettuce. Similarly, you have different types of variables for numbers, letters, lists and dictionaries.

String
Strings are used to store letters or alphabets. While assigning a string value to a variable, you should put either single quotes' ' or double quotes" ".
Example:

local breakfast = "Toast"

Numbers
Numbers are, well, numbers! Anything containing numbers including 0-9 falls under this category. You can also perform calculations with it.
Example:

local myFavouriteNumber = 911 
local add = myFavouriteNumber + 89 
print(add) --prints the sum of 911 and 89, which is 1000
lo

Now you might be asking, well then why not do

local myFavouriteNumber = "0" --0 is a string value here...

It is a way to do it, but here it is a String, so if you try to do arithmetic operation such as multiplication, division etc… it will result in error. That is because strings were never meant to perform calculations, for that we use the number data type.

Bool
Bools are simple- true or false. That’s it, it defines whether a condition is true or false.
Example:

-- conditions made by my mom
local goToSchool = true
local eatCandy = false

Arrays
Arrays are like lists, like the ones you carry while going shopping. It stores multiple values which can be of any type.
Arrays can be defined by enclosing your desired values with curly braces ie:{ }. Each value inside an array should be seperated by a comma.
Example:

local groceryList = {"Bread", 19, false} -- it contains string, num and bool

Dictionaries
A dictionary has a key and a value. Like arrays, dictionaries are created with curly brackets {} .Different key value pairs are stored on separate lines followed by a comma.
Example:

local food = {
    ["Turkey"] = "Yummy!",
    ["Do you like veggies?"] = false,
    ["How many plates do you have?"] = 0
}

That’s it basically it! Congratulations! You just made your first step into becoming a front page game scripter!

P.S: This is my first tutorial and I hope to continue it further, so if you have any feedback, feel free to comment down below. Hope this tutorial helped you in some way

  • This was a great tutorial!
  • This is an okay-ish effort
  • Nah, this is complete trash

0 voters

14 Likes

I already knew how to program in Lua, but I feel like this would help some people learn how to do it! Make sure you actually teach them Lua, not LuaU since you stated in the title that you were using Lua, which is different from LuaU in various ways, like adding two numbers and how to add delays to your code.

-- LuaU

local number = 1
number += 1

-- Lua

local number = 1
number = number + 1

LuaU is better than Lua in multiple ways, so I recommend changing the title to either have LuaU in it, or just “How to code on roblox” or something simple, still it’s a nice tutorial.

2 Likes

Yea. this was aimed for a beginner level. For the title, imma change it rn.

1 Like

Just having it be “Roblox Scripting” it’s not perfect, but it’s a lot better than just “Lua”, if you’re going to talk about more LuaU focused stuff, then the current title works fine, though specifying the difference between Lua and LuaU should be done at the beginning of the topic, since many beginner developers don’t know the difference between them (my self included), but it’s not mandatory.

Also, I’ve learned that tutorials for more high end programming concepts and more specific things make learning it a bit harder than just having documentation and a good mindset, so I would recommend not to go into specific things like path finding, ray casting, and advanced CFrame functions, since the user might only know one way how to do something, and they might not learn a better way because it’s so specific, and high end programming concepts just get learned “naturally”, once you get bored of “normal” code, you start to learn harder stuff.

2 Likes

As far as DevForum tutorials go, it’s not that bad.

Some random feedback to maybe take into account.

  • Introducing value types as “types of variables” could be confusing, because not every value is wrote directly to a variable.

  • At the top you claim you will come back to local, but never mention variable scopes anywhere throughout the rest of the tutorial.

  • May be worth mentioning how Lua actually assesses values. Every value type in Lua can and will be assessed as a conditional value. This means that nil and false act in effectively the same way, and that numbers are assessed as true (alongside every other combination). Statements are conditionally assessed consistently. This is worth mentioning because a future learner may gain a misconception of what is actually happening when they write their conditional statements.

  • Introduce arrays and dictionaries as the same value type (table), because they are. Dictionaries are an extension of arrays.