Need a quick rundown on Varibles

hey i need help with something i need a quick rundown on varibles and how you use them and how they work i know theres is tutorials but cause i have autism i learn slower than others so and its hard for me to get can someone teach me so i can get how varibles work thank you who ever so this.

Variables (roblox.com)

What? I mean variables are just basically something you can define, think of it as like your own custom Dictionary

local MyWord = "This word is evil!"
print(MyWord)

You should make variables local so that you can use them at any time within the script, and for good practice as well

Of course if we wanted, we can make Variables a String (Or MyWord), a Number (1, 50, 200, maybe even 1,000,000), or a simple Bool (True/False)

Variables can also be used to reference Instances inside your game as well, so say I have a part inside the workspace here:

local Part = workspace.Part
print(Part)

This would be valid as a Part would be in the workspace when we run this script

1 reason why we create variables, is so that we can reference it much easier instead of having to do something like this

Example A:

workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 1
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 0

Now obviously, WE HAVE A LOT OF FOLDERS HERE which can take up a lot of space, but say we look at Example B:

local Part = workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part

Part.Transparency = 1
wait(1)
Part.Transparency = 0

This would be much easier than having to reference so much Folders all individually :wink:

Overreaction Bonus:
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 1
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 0
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 1
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 0
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 1
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 0
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 1
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 0
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 1
wait(1)
workspace.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Folder.Part.Transparency = 0
wait(1)
2 Likes

On top of this you can set variables to tables:

local table = {
   Item1 = workspace.Part1, -- Item1 is a key in this case, and workspace.Part1 is the value
   Item2 = workspace.Part2
}

print(table.Item1) --This looks at the table "table" and finds the key called "Item1", which returns the object Part1 in workspace

You can also do something like:

table = {1, “Hello world!”, false, Part} --this is called an array which works a bit differently than the dictionary from the previous example. Instead of doing table.key to get the value from the dictionary, you can do table[1] or table[2]. So you could do something like:

table = {1, "Hello world!", false, Part}
print(table[2]) --This will print "Hello world!"

its basically a short cut.
for example if ur using game.players.localplayer a lot it can be annoying to type it out every time so instead u make it a variable

local plr = game.Players.LocalPlayer
plr.Character:Destroy()
plr:LoadCharacter()

so instead of typing out game.Players.LocalPlayer every time im gonna get the plr i make it a variable to not have to type it every time
in that script “plr” would act the same wat as game.Players.LocalPlayer
would bc that’s what i assigned the variable to do

Yeah I’m aware

I just want to properly break it down in simple terms (Or just talk about the general concepts on what variables exactly are)

The Roblox Education site is a great resource to learn at your own pace and provides examples that you can follow to build an entire game from the ground.
https://education.roblox.com/en-us/resources
This section covers variables:
https://education.roblox.com/en-us/resources/variables-properties

In General “Variables” are memory locations to store values.
When you declare variables you are given a memory location to use from the free memory available.
For example
local Integer = 5
This basically means you have stored the value 5 at a certain memory location of the computer.
“Integer” is the identifier(the name you provide to that variable).