generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: NotAshley's Scripting Tutorial #4 - Variables

Previous Thread :: Next Thread 
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
19 Mar 2016 03:13 AM
Chapter 4 - Variables

Previous chapter: Configuring Studio
http://forum.roblox.com/Forum/ShowPost.aspx?PostID=185557507

We're finally here! After all that background knowledge and preparation, all the boring stuff is out of the way. It's time to begin learning how to write your own Lua scripts. From this point onward, all tutorials will be on different Lua features and how to use them in your code. It may not be immediately obvious how to use this to make whatever you want, but I assure you, if you stick with it, it will become clear.

As I've said before, we are going to begin by learning Lua and its syntax. We won't learn how to interact with ROBLOX until a few chapters ahead. At the end of this Lua introduction, you will understand the basics of the language itself. You won't be able to do much, but you will then be able to apply the knowledge to ROBLOX Lua and learn it much easier.

So, variables. This is an extremely important concept in every programming language. In fact, this isn't specific to programming; it's also an essential concept in mathematics! If you've ever done algebra in grade school, then you already know exactly what variables are.

They are special identifiers (usually letters or keywords) that represent values and can be used instead of them. For example, if x equals 25, the answer to the math equation "x + 5" is 30. In that example, the letter x is a variable, and that variable holds the value of 25.

Remember numbers, strings, and booleans? Variables in Lua are special user-created keywords that hold data types such as those. They work the same way as mathematical variables, only they're more diverse and less bland. Makes sense, right?

Let's give an example. Load up a new script in Studio and follow along if you'd like. To create a variable with the keyword "bob" and have it hold a string value that says "Hello, my name is Bob," you'd make a new line in a Lua script that says this:

bob = "Hello, my name is Bob!"

Remember how in the chapter on data types, I explained that the string data type is represented by text inside of quotation marks? Well now you see what that looks like!

What's actually happening in that line is quite intersting, and knowing it will really help you out in the future. Since it would take a whole other series of tutorials to explain in detail the concepts behind how computers work, I am going to simplify it:

Lua has a temporary pool of memory that it uses to store information. It's empty when the script starts running, and it empties when the script stops running. When Lua wants to store something in that pool of memory, it has to pick out a chunk of space and basically tell the computer "hey, I want to store something here!" The computer then opens up that space and allows Lua to store some data inside of it. When Lua wants to retrieve that data later, it knows exactly where to look. This procedure is called memory allocation in computer lingo, but you don't need to memorize that unless you want to.

When you set a variable, that's exactly what you're telling Lua to do. Let's use the bob variable as an example. When that line runs, Lua opens up a new area in its memory to store a string that says "Hello, my name is Bob!" To make it easier for you to use it later in the code, it memorizes the bob keyword you wrote. Now any time you use the bob keyword in any sort of instruction, Lua will know to find that stored string and use it instead.

Unfortunately, the keywords that you set have a few limitations, and you should remember those limitations. Breaking them will confuse your script and cause it to throw errors at you. A variable name needs to follow this list of rules for it to be valid:

- It contains only letters, numbers, and underscores
- It has no spaces or other whitespace characters
- It doesn't start with a number

The first rule applies because special symbols are used to represent things like mathematical operators. For example, Lua would have no way to understand that an attempt to set a variable with the keyword "apple+pie" isn't supposed to be an equation that adds whatever numbers are associated with the keywords apple and pie. In fact, if there was no variables set with those two keywords, Lua would stop the script and print a confusing error about it to complain. That error would be pretty hard to decipher if you thought you were just setting a variable.

The second and third rules exist for similar cases of ambiguity. When Lua is reading the instructions in your script, it looks at each individual keyword and symbol, separating them using spaces and newlines. If you add a space in your variable name, Lua will see two different keywords. It will then get confused because it doesn't expect to see a keyword after a keyword, it expects to see more instructions on what to do. Similarly, Lua is equipped to handle scientific notation, so if you put a number at the beginning of a keyword, it won't know how to cope. It won't be sure whether you are trying to make a keyword or a fancy number.

Here are a few examples of setting correct variable names:

best_number = 5
bestNumber2 = 123
AshleyIsC00l = true
youKnowTheRules = "and so do I"

Here are a few examples of setting INCORRECT variable names, which will result in errors:

2spooky = "4me"
chance of survival = 0
-+FancyVariable+- = "swagger swagger swagger"
canJump? = false

That's all there is to it! Any value of any data type can be set to a variable, and using that variable later will call upon the data that was stored in it. It might not seem very useful right now, but trust me, you'll understand soon enough. Once we get into the intermediate side of scripting, variables can help to represent data that you don't know the value of (e.g. the chat input of a player), or to store massive clusters of information that you repeat several times in the code (how easy it would it be to change one line instead of hundreds?).

In the next chapter, we will talk about built-in functions, and how you can use them to perform basic tasks using data types and variables.

See you next time!


Report Abuse
HarrySnotte is not online. HarrySnotte
Joined: 27 Jun 2011
Total Posts: 2854
19 Mar 2016 03:59 AM
Is anyone actually reading this book? wtf
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
19 Mar 2016 04:08 AM
Stop spamming, you already posted this garbo
Report Abuse
NotAshley is not online. NotAshley
Joined: 16 Jan 2014
Total Posts: 14257
19 Mar 2016 07:33 AM
@Flux this is literally the only time I posted this chapter...


Report Abuse
c9_io is not online. c9_io
Joined: 15 Feb 2014
Total Posts: 2642
19 Mar 2016 07:36 AM
Wiki is here for you


#code "Lua ~ PHP ~ CSS ~ HTML for me at least."
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
19 Mar 2016 07:54 AM
You forget to mention locals are faster and tables are pass-by-reference :)
Report Abuse
ExtremeBuilder15 is not online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
19 Mar 2016 09:19 AM
You should probably include links to ALL the chapters, not just the previous one.
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
19 Mar 2016 12:59 PM
Extreme, it's called a linkedlist xDDxXdXDD
stupid joke
Report Abuse
TESTsubject0100 is not online. TESTsubject0100
Joined: 18 Aug 2014
Total Posts: 9286
19 Mar 2016 02:52 PM
Great tutorial!


No offence, but there's a wiki...
Report Abuse
SourHaze is not online. SourHaze
Joined: 15 Dec 2008
Total Posts: 588
19 Mar 2016 05:21 PM
@warspy
I don't think she covered locals or tables yet

@extreme
how are you supposed to link forward to a thread that doesn't exist yet? durr


Report Abuse
superpants2020 is not online. superpants2020
Joined: 10 Feb 2013
Total Posts: 804
19 Mar 2016 05:48 PM
^ logic.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image