RoGo Magazine - Lua Tutorial – Prints and Waits – Lesson 1

.

Posted in Tutorials

September 1, 2014 at 6:50 PM

Lua Tutorial – Prints and Waits – Lesson 1

By Spongebobworldonline

Editors: Sparker22, Dornbaum, Joshuats and Newyearcountdown


Hello, and welcome to my scripting guide. If you can remember, a few years ago, I used to publish my scripting guides back when we published magazines. The problem with that was that it was a lot of work to put together and the scripting section was actually quite difficult to lay out into the magazine. We then decided to stop doing this so it was put off. Fast forward a year and we are now moved to the website, which means it is much easier to work with and to create new content.

Every week I’ll try to make a new tutorial, and I will be going in order of what I feel is the most important stuff to learn first, then moving on to more advanced stuff. So, I guess the first thing to do is to learn how a script actually works.

In ROBLOX there are currently three types of scripts you can use.
 Script
 LocalScript
 ModuleScript

A “Script” is run server-side, while a “LocalScript” is run client-side. (Hence the name “LocalScript”, as it is, in fact, local.) The advantages of having code ran client-side is that you can control client-side things, such as a player’s camera.

A “ModuleScript” is more advanced. They can be used to make libraries, which we will be getting into in a more advanced tutorial.

We’re mostly going to be using normal scripts, but later on we will be using local scripts.


When people hear about programming languages, they often think you have to be very smart to understand them. In reality, it’s actually really easy to do once you understand some key stuff about programming. When you think about it, all you are doing is telling the game what you want it to do. Think of it like an actor’s script. The producers of a television show make a script, and the actors must follow it. The same applies for Lua in ROBLOX. You are telling the ROBLOX engine what to do, when to do what, and vice versa.

A script reads from left to right, and additionally from top to bottom. The code you have at the top of the script runs first. Here’s an example of a simple script. I will go over each function with you.

print(“Hello”)
print(“My”)
print(“Name”)
print(“Is”)
wait(5)
print(“Brandon”)

print()

A “print” is a core function of Lua. In ROBLOX, while scripting it’s a good idea to have a window called the “Output” open as shown here.

LUA1print()

The output is one of the most important things you use while scripting. It allows you to see if you made any errors in your code, and it will show you where they are. Additionally, you can see any text you print. Whenever you use the print function, the player can’t see it while playing your game, but it’s useful for testing things while making new code. (You will see why in the future.)

It might seem pointless now, but it will come in handy for quick testing.

wait(number)

This is a function built in to ROBLOX’s version of Lua. In the script, you may have noticed that the 5 was not inside of quotation marks. This is because the wait function only accepts number values. In the next lesson, I will be talking more about data types in your code.

What the wait function does is pause your code for the amount of seconds you command it to wait. For example, putting “wait(5)” like we did in our script above will make your code wait for 5 seconds before continuing, and finally printing “Brandon”.

How-To: Make a Script and Run It

  1. Open up ROBLOX Studio. If you don’t have it, you can download it here.
  2. Open up an existing or new place in “Build” mode.
  3. Ensure that you have the following windows open:
    • Explorer
    • Output
    • Properties
  4. In the Explorer, right-click the Workspace, go to Insert, then “Script”.
  5. A window should appear. This is where you code. If it doesn’t appear, just double click on the Script in the Workspace.

To run the code, just click the Script once so it’s selected. Then in the Properties menu, toggle the “Disabled” value to true, then false again. This turns it off then back on.

I understand that this tutorial was not very interesting, but before we can start doing interesting things we must learn the basics.

In this lesson we learned what scripts do, the different types of scripts, how prints work, how waits work, how to set up a script, and run it. In the next tutorial we will be learning about variables, functions, and data types.

Thanks,
Brandon