Easy Written By kitrank

Variables Created on: 01-01-2019

Learning about variables

Introduction

This tutorial is the third in a series, and will assume you have read the previous tutorials and understand any previously covered concepts.

Variables

What are variables? If you have had Algrbra in school before, then you probably have at least heard of them. In Algebra and in Scripting, they are the same concept, just used differently which can be confusing.

In algebra, variables are usually hidden values that you have to find to solve the equation. In both algebra and scripting, the main concept behind variables is that the variable, usually defined as x in algebra, is used to store a value and is used to call that value by the variable name.

Here is an example of a variable in scripting:

local variablename = 10

We declared the variable "variablename" and set it equal to 10, a number value. Now usually the variable name would be shorter, but I made it variablename to demonstrate that variables can be named about whatever you want. I said about because the name can only include upper/ lower case letters, underscores, and numbers, but the first character cannot be a number. Variables can be set or assigned to any type of value, here are some of them:

local a = 10 -- Number/Integer Value
local b = "String" -- String Value
local c = {} -- Table Value
local d = false -- Boolean Value (true/false)
-- etc

There is one more type of value variables can be assigned to, which can be very practical. That is an Object value. Object values are used to reference objects in the place, and are often used when you need to repeatedly reference the same object over and over again. For example, say we had a specific Part in the Workspace and wanted a shorter name for it:

local Part = game.Workspace.Part

Now we could just say Part instead of game.Workspace.Part when we are trying to reference it. Now say we wanted to change two properities of the Part in Workspace, such as Transparency and Reflectance:

local Part = game.Workspace.Part
Part.Reflectance = 0.5
Part.Transparency = 0.5

This is the equivalent of:

game.Workspace.Part.Reflectance = 0.5
game.Workspace.Part.Transparency = 0.5

We can also use variables when repeatedly using the same value, for example:

local Part = game.Workspace.Part
local x = 0.5

Part.Reflectance = x
Part.Transparency = x

Whenever you are repeatedly referencing the same Object or using the same value, you should define a variable for it. This will decrease the amount of typing nessacary, and if you ever need to change the value for all of them, you can just change the variable's value instead of having to change every value individually.

Local Variables

In the previous section, you may have noticed that I added local before the variable name. This means we are defining a local variable, which is only usable in the variable's scope, which is basically the section the variable is in. For example if we had an if statement and defined a variable inside, then tried to print the variable like so:

if true then
local x = 10
end

print(x)

Attempting to print x outside of the if statement, which is the scope in this example, would print nil instead of the value because it cannot see it from outside the scope. Note that local variables are faster than global variables, which are covered in the next section.

Global Variables

Global Variables are usable anywhere in the current script, but not in other scripts. There is a way to make variables accessible in any script however that will not be covered here. To declare a global variable, it's the same way as declaring a local variable just without the local before the name, like so:

x = 10

This variable can used anywhere in the script, reguardless of scope.

Conclusion

This is it for variables. It's a very basic concept, but you will be using them a lot once you really start scripting. If this tutorial was helpful, please give a rating, and thanks for reading!

Easy Written By kitrank

See kitrank's profile on Roblox

Discussion