YOUR CART

HOW TO SCRIPT ON ROBLOX LIKE A NINJA

The place to find roblox scripting help, tutorials and advice

We've scoured the web to find the best resources on how to be better Roblox scritper! Below you will find the resources we founf most useful when learning Roblox developing.

How to script on Roblox by AlvinBLOX

AlvinBLOX has a pretty cool channel that is jam packed with a range of Roblox scripting tutorials. Check out his website too: www.alvinblox.com

Nearly 100 Scripting videos

Over 3 million views

Clear instructional videos

Here's a How-to on scripting basics from ROBLOX

A short video on the basics on Roblox scripting by ROBLOX.

Ultimate guide to ROBLOX SCRIPTING WITH LUA

A neat video from Heath Haskins - worth a watch!

Introduction to Game DevelopmentHello everyone, in this first episode I’d like to introduce two important concepts in programming: variables and methods. In our code we need to be able to keep track of changing information, and this is where variables come in. So, a variable is essentially a name to which we assign some data. For example, we might assign the number 15 to the name ‘playerHealth’ at the start of the code. At runtime, in other words while the game is actually being played, if the player takes damage, we can update the value stored in the variable. An example of this would be the line: playerHealth = playerHealth - 5 which subtracts from the player’s health, by assigning the variable a new value equal to its old value-5.

When we create a variable, we need to specify what type of data it will be holding. The playerHealth variable, for instance, holds a number, so we might specifiy it as an ‘int’ variable. This means that it will only every be able to hold an integer, or whole number value. If we wanted it to be able to store non-whole numbers, what we refer to as ‘floating point numbers’ (since the decimal point can be placed anywhere), we would make it a ‘float’ variable instead.

Other examples of data types include ‘bool’ for storing true/false values, such as playerIsAlive; and ‘string’ for storing text, such as the player’s name. As you can see, when we create a variable we first specify its type, followed by a name of our own choosing. To assign a value to it, we then add an equals sign, followed by the value. Some data types require the values to be written in a certain way, for example string values must be enclosed in quote marks, and float values must end with the letter ‘f’.

Finally a semicolon is added to indicate the end of that line of code. You don’t necessarilly need to memorise all of this right away, the main take-away from this is simply that a variable is a name to which we can assign data, and that this data can be accessed and modified at runtime to reflect the changing state of the game. All right, let’s talk about the next concept: methods - also known as functions. Methods are a little bit trickier to define in simple terms, so I’m to explain them by way of an example. Imagine we write a bit of code for creating an explosion. So the instructions would be something like: play a sound effect, make a flash of light, and apply 10 damage to all objects within 5 units of the blast. By the way, anything that follows two forward slashes on the same line is a comment in C#, in other words it does not affect the code, but acts rather as a note to oneself or to other programmers.

So I’m using them in this video to indicate lines which are not actual code. Now our game might have various objects that explode. Cars, barrels, etc. We could copy-paste the explosion code for all these different objects. However, if we then wanted to make a change to the instructions, for example add smoke, we’d have to remember to go through all the copies and make the change there as well. You can imagine this would get very tiresome, so as a rule, programmers tend to severely dislike code repetition. What methods allow us to do is define a name to which those instructions belong. So we could create a method called Explode(), and put the instructions inside of there. Now we can replace all this duplicate code with a single call to that method. Let’s extend this explosion example a bit. Currently we have hard-coded values for the damage amount and the explosion radius.

But what if we want an exploding car to be more powerful than an exploding barrel? Instead of saying ‘apply 10 damage to objects within a 5 unit radius’, we could say ‘apply damageAmount damage to objects within blastRadius of the blast’. We have replaced the hardcoded values with variables. We now change what is called the method’s ‘signature’ to reflect this. So inside these parentheses following the method name, we define the two variables that this method uses. Notice, however, that we don’t give the variables values. Whenever we call the method now, we can supply whatever values for those variables we want. Here when the car calls the explode method, damageAmount = 8 and blastRadius = 12 Whereas when the barrel calls it, damageAmount = 5 and blastRadius = 10 So we’ve seen that we can pass data into a method, but we can also return data from a method if we want.

Say we want to be able to generate random names for the different characters in our game, so we make a method called GenerateRandomName. This starts off by creating an empty string variable (remember that’s for storing text) called randomName. Then we randomly add some letters to this variable.

Now we have a random name, but we’re not done yet. Whoever is calling this method will want to know what the result is. So at the end of the method we add the keyword ‘return’, followed by the variable our result is stored in - randomName. Just like we have to specify the type of variable we’re creating, we have to specifiy the type of data that a method returns. If it doesn’t return any information, as was the case with out Explode method, then we just have ‘void’ before its name.

GenerateRandomName returns text, so we’d add ‘string’ before the method name. Now, whenever a name is required, we can say string name = GenerateRandomName(); What we’re doing is setting name = to the value that is returned from that method. So to recap, a method is a means of isolating code that performs a specific task so that it can be called from multiple places. It can optionally take in data to work with, and can either return void, that is nothing at all, or it can return a piece of data. I hope you found this video helpful. In the next episode we’ll be talking about classes.

ClassesHi everyone, welcome to episode 02. In the last episode we discussed variables and methods, and today we’re going to be looking at classes. Simply put, a class is a means of grouping related methods and variables together. Creating a class can be as simple as writing the keyword ‘class’, followed by the name of the class (for example Player), and then adding an opening and closing brace between which comes all of the code. Classes aren’t only for organisation, however, they also allow us to reduce code repetition through something known as inheritance. Let’s take a look at an example: Say we’re trying to recreate the animal kingdom in our game.

We might have a class for mammals, and a class for birds. If we start writing code in both of these classes, we’ll soon realise that they have a lot in common. For example, both birds and mammals need methods to define eating and sleeping behavior. Rather than have the same code in both classes, we could create a new class called ‘Animal’, which has this code in it. Both Mammal and Bird can then inherit from Animal, meaning that they both get access to this functionality. In the Mammal and Bird classes, we can then focus only on what is unique about them.

For instance, mammals give live birth to offspring, while birds lay eggs and can also fly. Now if we wanted to create an elephant in our game, it could inherit from mammal. Straight away it has all the functionality of other mammals: eating, sleeping and giving live birth to offspring. All the elephant class then needs to define is its unique ability to control a trunk. As you can see, this system of inheritance - going from general, shared behavior, to specific behavior, can be a very powerful tool. However, in some cases we’re likely to run into some problems with this system.

For instance, if we were to create a penguin, we’d find that while it shares some behaviour with other birds, it can’t fly; instead it swims. Let’s have a look at an alternate way of arranging things using the concept of composition instead of inheritance. The idea here is to split the classes up so that each one implements a single behaviour; so one class for swimming, another for flying, and so on. We can now assemble any animal we like by simply combining the relevant components. Furthermore, if these components are generic enough, we could use them again in future projects.

Ultimately we aim to keep our code flexible, so that it is easy to introduce changes later in development, as well as to minimize code repetition. Inheritance and composition are both tools which enable us to do this. Alright, now in Unity, we generally use classes in one of three ways.

We’re going to discuss two of them in this video, and the third will form part of a later episode. The first is as a behavioural component. These are the scripts that we attach to the objects in our game to drive their behaviour. These classes must inherit from Unity’s MonoBehaviour class, which simply contains some common object functionality.

One example of this functionality is that if we create a method with the name ‘Start’ in our class, monobehaviour will ensure that it gets called once at the start of the game. Likewise if we create a method named ‘Update’, monobehaviour will make sure that it gets called once every frame while the game is running. So say I create an enemy class. The colon after the class name indicates that this class inherits from MonoBehaviour. Inside the update method, I might create a bool variable called canSeePlayer, and set it to false.

Then I’d have some code that sets canSeePlayer to true if the player is within a certain distance. Finally, I’d have an if statement (which we’ll learn more about later) to say if the canSeePlayer variable is true, then attack the player.

Now we’ve only written one enemy class, but of course that doesn’t mean that there can be only one solitary enemy in our game. Each enemy object can have its own copy, called an ‘instance’ of the Enemy class. Importantly, the variables inside the different instances can have different values while the game is running.

In other words, while the game is being played, one enemy’s canSeePlayer bool might = true, while at the same time, another’s canSeePlayer bool is false. These sorts of variables are called instance variables, since they belong to the individual instances of the class. An instance variable is the default type of variable, so all variables that we’ve seen up to this point have been instance variables.

The opposite of an instance variable is called a class variable, since it belongs not to one particular instance, but to the class itself. We create class variables by adding the keyword ‘static’ when we create the variable. To illustrate the difference betweeen class and instance variables, I’m going to create a static integer variable called numberOfEnemiesAlive, as well as a non-static integer variable called instanceExample, inside the Enemy class.

Say we have 5 enemies, and at the start of the game, each of them adds +1 to both numberOfEnemiesAlive and instanceExample. Now, when the game is running, the value of numberOfEnemiesAlive is equal to 5, while instanceExample has a separate value of 1 for each of the 5 Enemy instances.

Let’s say I wanted to access the numberOfEnemiesAlive variable from another class. First we’d have to add the keyword ‘public’ in front of it, to make it accessible to other classes. Now, in my other class, I could access the numberOfEnemiesAlive variable by simply writing Enemy.numberOfEnemiesAlive. In other words, the class name, followed by a dot, followed by the variable name.

However, if I wanted to know the value of the instanceExample variable, do you think I could do the same thing? No. In that case the computer would ask, which instance of the Enemy class do you want me to fetch the value from? And by ask, of course, I just mean that there would be an error. Hopefully this example has made the difference between instance and class variables clear. Note that the same concept of static and non-static applies to methods as well. This leads us to ‘way in which classes are used, number 2’. Static classes. Making an entire class static has two main implications for us to consider at the moment.

The first is that all of its methods and variables have to be made static as well. The second is that we can’t attach this class to objects in our game since ‘static’ forbids us from creating instances of it. On the other hand, as we saw with the numberOfEnemiesAlive example, the restriction of making something static means that it is very easy to access from other classes. Because of this, static is extremely well-suited to what we call Utility classes.

An example of a Utility class would be a static class called Maths. Inside of this, we might define various things, for example a static float containing the first 10 digits of pi, or a static method to calculate the squareroot of a given number. Once again these are all made public, so from any other class, if we want to know the value of pi, we just have to write Maths.pi, or similarly if we need to calculate the square root of a number, we can write Maths.squareRoot(x); All right, that’s all the new information for this episode.

Let’s do a quick recap: Classes are used to group together related methods and variables. We can expand on the functionality of a base class, such as Animal, by inheriting from it. Alternatively, we can have each class define a single behaviour and build more complex, composite behaviours out of these elements.

When we attach a class to an object in our game, we are creating an instance of it that belongs specifically to that object. Values of the variables inside that instance can be different to the variables of other instances of the same class.

The exception to this is if the variable is static, in which case it belongs to the class, not any specific instance. Making variables and methods static is especially useful in Utility classes such as a maths class, as they can be accessed from other classes without having to get a reference to a specific instance. I hope you enjoyed the episode. As always, feel free to ask me anything you’re unsure of in the comments. Until next time, cheers.Unity OverviewHello everyone, welcome to episode three. In the last two videos we’ve covered some of the fundamentals of programming, such as variables, methods and classes; so today I’d like to give a quick overview of the Unity interface, so that we can start experimenting with it in the episodes to follow.

This is the default view when we first open Unity. We can customise the layout, for example I might drag out this game window so that it sits next the scene view, and I could do the same with the console window so that it's over here next to the project panel. And I might also just resize some of these panels because they’re taking up a lot of space. And then if we want to save our custom layout, we can just go to the layout dropdown on the top right.

And then of course we can switch back to default at any time we want. All right, so the unity editor is made up of these 6 main panels. The hierarchy, the scene, the game, the inspector, the project and the console. The hierarchy shows a list of all the objects that are currently in our scene. So At the moment I have a camera object and a lightsource.

If I were to add in a default cube, that would show up here, as well as in my scene view, where I can move it around, or by changing to the rotation tool in the top left, I can rotate it, and scale it. All of these have shortcuts of course: W to move, E to rotate and R to scale. In the scene view I can zoom with the mouse wheel, and I can hold down alt + left mouse drag to rotate. and middle mouse drag to pan. This little gizmo in the top right, allows us to rapidly switch between viewpoints. We can also change between perspective and isometric mode, by toggle the cube in the middle.

The game window shows a preview of our game, seen from the eyes of our camera object. We can press the play button at the top to start and stop the game, though currently not much will happen as we haven’t programmed anything yet. The inspector shows us information about the selected object. Remember how in the last episode I spoke about composition, a way of building functionality out of multiple classes that each define a single behaviour?

Well here we can see an example of this at work. Our cube object has 4 components attached, all of which are simply classes that Unity has written, and added to this object to make it into a cube. If I remove the box collider component, it loses collision functionality; if I remove the meshrenderer component, it loses rendering functionality.

The transform component is what allows us to move the object around, as well as rotate and scale it. The project window contains all of the assets for our project. So in a full project we’d have lots of scripts, and 3d models, audio files and so on listed here. We can drag things from our project file into our scene.

For example, let me add a simple flash class that I created earlier to the project. If I drag this onto the cube, you can see that it appears as a new component in the inspector.

So our cube now has an additional behaviour. If I press play, we can see that take effect. It’s worth getting used to the idea that while we’re in play mode, our changes are not permanent. I might remove this script, for example, and change the position of the object, but as soon as I exit play mode, it reverts back to how it was before. This behaviour is beneficial, however, as it allows to try out new things while the game is running, without worrying about ruining our work.

Finally, there is the console window. The console allows us to print messages from our scripts, containing information about, for example, the value of a variable. This allows us to track down bugs in our code, when things are not behaving as intended. It is also here that we will receive error messages, if we program something incorrectly. The last thing I’d like to mention is that we can have multiple scenes. So if I save this scene, I’ll maybe call it Cube Scene, then I can go create a new scene. In here I’ll make a sphere object, and then save it and call it Sphere Scene. Now I can switch between these two scenes by double clicking on them.

And obviously there are ways for us to switch between them using code during our game as well. So as you can imagine, this is very useful for when we want multiple levels in our game, or even just for separating the game menu from the game itself. Ok, so you hopefully now have an understanding of the broad elements of the unity editor. In the next episode, we’ll start applying everything we’ve learned up to this point by programming some simple scripts.