How to use variables

In this tutorial you'll learn about variables

by Mr_Ther

Author Avatar

Hello, I am therDanTDM and today we'll learn all about variables.

A variable means something that is liable to change. Variables are

like shortcuts to an object in the game. There are two

types of variables in Lua:

*global variables

*local variables

myvariable = workspace.Part --global variable
local myvariable = workspace.Part --local variable

Global Variable:

myvariable = workspace.Part --A variable can be named anything. This
--is a global variable.

A global variable is a variable that does not need any declaration.

It can be accessed from any block of code and is only used in an

enclosing function.

Local Variable:

Unlike global variables, local variables have their scope limited to

the block where they are declared and they need the declaration

'local' before being used.

script.Parent.ClickDetector.MouseClick:Connect(function()
local myvariable = workspace    .Part

end)
--This is a local variable  used in a function. Here using a global
--variable will throw an error

Extra:

--If you use a variable, you don't need to type the entire
--thing, just the variable name. Example:
part = workspace.Part

part:Destroy()

View in-game to comment, award, and more!