Variables: Difference between revisions
>Mindraker Absolute beginners |
>Mindraker |
||
Line 39: | Line 39: | ||
x = 1 | x = 1 | ||
myname = "johndoe" | myname = "johndoe" | ||
</pre> | </pre> | ||
Revision as of 00:58, 2 October 2008
Introduction
This guide is for absolute beginners. It is intended to familiarize you with Variables in Lua. If you haven't already, please see Your first script as a beginner tutorial.
What are we trying to do?
At the end of this guide, you will be able to:
- Know what a variable is
- Create a variable
- Assign a value to a variable
- Use a variable in a mathematical equation
Setup
You will need to open Roblox Studio. Once you have done that, you will need to click "My ROBLOX", select your map, and click "Edit". At this point, you should see your familiar map.
You will need some essentials in Roblox Studio to make this work:
- The Output window:
- The Explorer window:
- The Command bar:
- To make sure you have the Output window, click View / Output.
- To make sure you have the Explorer window visible, click View / Explorer.
- To make sure you have the Command bar visible, click View / Toolbars / Command.
Discussion
A variable is a word or letter to which you can assign a value. For instance, in algebra, you can give "x" the value of 10. In lua, there's a little more liberty -- not only can a variable be a number, but it can also be a word, like your name:
x = 1 myname = "johndoe"
Notice that the word "johndoe" is in quotes, but the number 1 is not in quotes. That's because "johndoe" is considered a String. In short, you can perform arithmetic on numbers, but not on "johndoe".
Arithmetic can be performed on variables, just as on numbers. If x is equal to 2 and y is equal to 3, then x+y=5. Let's try that in the scripting section.
Scripting
Open Roblox Studio and make sure that there aren't any other scripts in the Explorer window. We're going to start off with a fresh script.
- Click Workspace in the Explorer Window.
- Click Insert > Object > Script
- Double click the newly inserted script.
- A blank window should open up.
- Insert the following script:
x = 2 -- this assigns the value of 2 to x y = 3 -- this assigns the value of 3 to y print(x+y) -- this prints the sum of (x+y)
- Test the script by pressing the play button File:Play.JPG
- Your Output bar should show the number 5, which is the sum of x+y.