User:Trappingnoobs/LearnLua

From Legacy Roblox Wiki
Jump to navigationJump to search
What's LearnLua?
LearnLua is a peice of literature by myself. I'm writing it from a new scripter's point of view- They want to learn, but they want to make good stuff as they go along. It's updated quite often, so check back! :D


LearnLua- With working examples



REQUIREMENTS

  • A LOT of time.
  • A LOT of patience.
  • A LOT of revision.


RUNNING SCRIPTS WITH LUA IN ROBLOX

First, click Start. If you don't use edit mode, FOLLOW THESE WELL.

It may seem similar to what you usualy do, but it's not.

Windows 7

Open the start menu. (The blue circle with windows icon in) Click "Search programs and files", type roblox. Click Roblox Studio. Skip to after windows XP now.


Windows Vista

Open the start menu. Click "Search programs and files", type roblox. Click Roblox Studio. Skip to after windows XP now.


Windows XP

Open the start menu. Explore until you find Roblox Studio Open that.


WHAT TO DO AFTER OPENING ROBLOX STUDIO

Ok, first decide:

I want to save my tests to (one of) my place(s)

->Find the place and click Edit

I just want to keep my tests on my computer.

  • If you have the menu with File etc on, click the blank paper.
  • If not, open any place in "Edit" and click the picture of the bullet points. This brings up your awesome tool menu.
  • Now click the small x. It's not that big red one, it's just under your main tool menu at the top, near the two tabs (One will be the web browser (IE) that ROBLOX Studio comes with, and one will be the game.)
  • Now click the blank paper. This'll bring up an empty place. I'm assuming you have some basic knowledge of using things like: "File->Save As" if not, you should reconsider learning to script. You'd be better off following a more basic computing tutorial, such as how to save.

HOW TO SCRIPT

  • Click view->Explorer
  • Click view->Properties
  • Click view->Toolbars->Command
  • Click view->Output

Arrange these windows by clicking the top and dragging. Get these how you want them.

Now click the plus on Workspace in "Explorer". Once you've done that, select Workspace and click (At the top) Insert, then click Object, then find script and click ok.

This script will saying something like:

print 'Hello world'

when you double click on it. Erase that, and you're ready!

To run what you type, find the play button, above the game display window.

Once you're done testing the script, click the red stop button which reverts everything.

If you allow the script to run

print 'hello world'"

you'll see the words "Hello world" in the output.

One line scripts can be ran in the command bar, and it runs even if it's not playing, for things like testing a loop.


TOPIC ONE: VARIABLES

So, in any language, the first thing you master should be variables. Along with every single other scripting and programming language, lua is COMPLETELY useless without variables- You can barely make

  • ANYTHING* without them. Beleive me, unless you want undynamic (Doesn't react to anything; just does the same thing each time it runs),

boring code, feel free to skip this section.

So, let's give some information to you.

First, the bool type. Bool, or Boolean, represents true or false. Lua is a dynamically typed language, which means you don't need to TELL it that you're using a boolean, it'll figure it out itself.

Don't worry about keywords you might not understand. I'll explain most as I go along. If you do understand, don't feel.. Not sure what the word is, but I have to explain for the newer learners.

"Dynamically typed" simply means it'll figure stuff out on it's own. You could go into complex definitions for it, but not right now.

So, bool = true or bool = false. Simple, right?

Let's give you some examples. Execute these if you wish.

Don't worry- Print isn't anything to do with a printer. It simply adds some text to the output.


print(true)   
    --True
print(false) 
    --False

x = true
    --We declare a peice of memory to remember that x is true.
print(x)
    --We add the value of x to the output. It should say true.

z = not true
    --Not true is false. not is a lua keyword.
print(z)
    --false
y = not z
print(y)
    --true

Note that we said 'x = true', 'z = not true', and 'y = not z'.

These are variables. Variables are basically memory 'slots' that remember things. Unless I change it, x will be true until the end of the script, z will be not true (false), and y will be not false (true).

not inverts whatever comes after it. It's realy straightforward, not true is false, not false is true.


So that's the basics of bool values. Now onto numbers.

  • An int is a number without a decimal point. It is short for 'Integer'.
  • By default, when you declare a number in lua it's a 'number' type, which has a decimal.
x = 5     --> 5     = 5
print(x)
    --5
y = x + 1 --> 5 + 1 = 6
    --6