Variables are a very helpful tool to use in Roblox Studio. With them, you can do things like randomizers, true or false statements, and much, much more. In this tutorial, I'll go over some uses for variables, and why you should use them.
Using variables, you can shorten the time it takes to write a script by doing a little something like this:
local Variable = game.Workspace.Part
So what was that?
Pretty much, I wrote the path to the object. Let me break it down.
First up, numbers.
Numbers in variables can be used for many things. For example, they can be used to count how many times a button was pressed, or to compare two different values.
This is a script to count how many times a button was pressed:
local PressCount = 0
local ClickDetector = game.Workspace.Part.ClickDetector
--These are the variables
function Clicked()
PressCount = PressCount + 1
end
ClickDetector.MouseClick:Connect(Clicked)
--Don't mind this. It's just to start the function, but it isn't very related to the variables.
Let's go through this.
At the top, you can see the variables. The ClickDetector variable is just to see if the part was clicked. What we want to focus on is the PressCount variable. As you can see, I've assigned it a value. This means that PressCount is equal to 0 at the time. When the function Clicked starts, you see this:
PressCount = PressCount + 1
This means that PressCount is no longer 0, it's now the previous value of PressCount plus one. So the first time this function runs, PressCount will be 1. The next time, it'll be two, and so on.
Got it?
Next, lets take a look at true/false variables and statements. A true/false statement is pretty much what it sound like. It's either true or false. Pretty simple.
local Clicked = false
local ClickDetector = game.Workspace.Part.ClickDetector
function Click()
Clicked = true
end
ClickDetector.MouseClick:Connect(Click)
--Did Click instead of Clicked this time so I don't get the variable and function mixed up
Now, after seeing the example for numbers, this should be a bit more familier. The variables are once again at the top, and the Click Detector at the bottom. Whats different is that instead of setting a number, we're setting a true/false variable. In this script, I haven't included a use for this, but I'll show some later on. Before, Clicked was set to false, as to say that the part hadn't been clicked. When we run the function, it is set to true, like we're saying that is has been clicked. Make sense? Good.
Now, I'm no expert in this subject, but I'm sure a few of you want to know about equations and math. So I'll try my best to give you the basics.
First off, regular math.
Here is a script that adds 2 numbers together, and binds it to a variable.
local NumberA = 5
local NumberB = 7
local Sum = NumberA + NumberB
print (Sum)
If we were to run this script, the output would be 12. But why?
Well, if NumberA is worth 5, and NumberB is worth 7, then the two of them together equal 12. Also, if Sum is equal NumberA and NumberB added together, then Sum = 12. That's why the output is 12. At the end of this, I put:
print (Sum)
What this does is it prints the data stored in the variable. For example, if Variable = 4, the data stored in Variable is 4. Simple, right? If you were to add quotation marks on either side of Sum, the output would be Sum. But if you just use parenthesis, you'd have 12 as your output.
(Yes, you can do this with subtraction and other equations.)
And now, my personal favorite. Variables and randomizers.
Now you might be wondering, "How in the world do variables and math relate to randomizers?" I'll have you know that they are very close.
Very.
So anyways, lets start with a simple 1 - 10 randomizer
local Number = math.random(1, 10)
print(Number)
So this simple script doesn't require a long explanation. The math.random(1, 10) picks a random number between 1 and ten. Since that's in a variable, Number would equal that random number. It then prints that number.
To change the min. and max, change 1 and 10 to your prefered numbers. What you want to do with the output is up to you.
So, what is a property? Its pretty much something about a part, or a GUI, or anything in the explorer. You can change most properties of something in the properties menu, but what we're focusing on here is changing them through scripts, with variables.
local Brick = game.workspace.Part
Brick.Transparency = 0.5
What this script does, is it changes the part's transparency to 0.5 when run. Remember the beginning of this lesson, when I showed you how to reference a part? This is related. This:
Brick.Transparency = 0.5
Is the same as this:
game.workspace.Part.Transparency = 0.5
Because if Brick = game.workspace.Part, then replacing any brick in the script with game.workspace.Part will achieve the same functionality.
Another way you can achieve this same function:
local Brick = game.workspace.Part.Transparency
Brick = 0.5
This time, Brick is worth one property: Transparency. So setting Brick to 0.5, also set Part's transparency to 0.5.
Now, time to knock out 2 birds with one stone.
Before, I said I'd include some scripts which showed a use for something, right? Well, this one's for a property and true/false statements.
local Brick = game.workspace.Part
Brick.Transparency = 0.5
Brick.CanCollide = false
When run, this script will make the brick half-transparent, and you'll be able to walk through it. Not all properties need a numeretic value, some require true/false, like CanCollide and Anchored.
Here, I'll give a few examples of how you can use some of the things you've learned.
Math.random) Can be used to generate random obbies, equations, and anything else. You can also use Math.random for drop rates! (I haven't done this myself, but you could probably do Math.random(1,4) and then use an if statement.)
Referencing Parts) Mostly just for making scripting shorter and easier. However, using script.Parent can be very useful when putting scripts in places that are copyed over to the player.
Properties) Exremely useful. Can be used to make a hidden area, timed jumps (using wait(), CanCollide, and Transparency,) or maybe a color-changing brick (wink wink.)
True/False) One pretty common use is setting a variable to true when an action is completed, then checking that variable later to check the status of the action.
Numbers) To count how many times an action has been completed, or maybe adding up gold in an RPG. Maybe a word count in a typing game, or a timer.
Variables open up a whole world of possibilities for scripting. I haven't even listed all the basics here, because that would be pretty long. Too long. But using what you've learned, I challenge you to make something interesting with your newfound knowledge. I'm sure you can.
Hey there! Thanks for reading this lesson. Keep up the good work, and I'm sure you'll get even better at scripting.