Coding Errors - Is it me?

Updated: Apr 1, 2019


Introduction

Roblox Studio uses Lua as its base coding language. It also integrates a lot of coding features into its IDE (integrated development environment), which make it unique and highly usable. As with all coding languages, they are only as good as the users who make use of them.

There is a term GIGO, meaning “Garbage In Garbage Out”. This refers to programmers entering code that the language simply does not understand, and some languages are VERY picky. Everything must be just right, or it won’t work.






Programmers must become proficient in the language to code quickly and effectively. The ability to write a large section of code, know what the outcome will be, run it, and have no errors is a developed skill, and might I add rare even for a seasoned pro. The smallest error such as using a capital letter where there should be a lower case letter, or using a semi-colon “;” where there should be a colon “:” can break the code and stop it from running.


Each language is different in how it handles such errors, and how it lets the programmer know they have done something wrong. The ability of the coder to READ and UNDERSTAND the error message allows them to quickly address the issue and get the code working as expected.


In this article I will highlight some of the more common errors a coder using Roblox Studio might come across and how to fix them. Hopefully this article will assist you fix those nasty errors, and get your game up for the world to see a bit quicker.


Error - Where is it?

The first step in dealing with errors in Roblox Studio is to know where to find it! The answer is in the “Output Window”. Any system messages and errors will be displayed here. If there are more than 1 message you will be able to scroll down and read them all.



View Tab - Click on Output Window


The Output Window

Let’s Create Some Errors!

I will begin with some very simple errors, what they mean, how to find them, and what to do to fix it. Any system messages and errors will be displayed here. If there are more than 1 message you will be able to scroll down and read them all.



Error - Red line under text

Before we even save, or run the program we can make an error! In this picture you see a red line under the code. This is called “intuitive text”. In the background a little program runs that monitors what you are typing, and aims to help by suggesting things to enter, or in this case that the line of code is incomplete. The fix for this error is to add a “)” at the end, and the red line will disappear. If your code has any red lines in it, it usually means you have typed something incorrectly.


Error - Blue line under text

This error is not as drastic as the red line error which will simply break your code. The blue line indicates Roblox Studio does not know what this is. Looking here we know it should say “print”. And you will notice from the first example that the word print is a bold blue. This means it is a “key or reserved” word in the programming language, and has a specific purpose.


In this example the word “rint” has no specific meaning. It is “undefined”. In this case we can clearly see that the coder intended to write “print” but mistyped the word. There are other causes of the blue line that we will look at next.


Here I have written my code, saved it and run it. I don’t get a big red error message at in the Output Window, I get the word “nil”. As stated in the previous example, Roblox Studio’s way of telling me it doesn’t know what something is, is to print the word “nil”. The blue line here means “you have not defined ‘myMessage1’, but it doesn’t break the code, so i’ll underline it in blue set it to nil and keep on going”.


Error - Not So Obvious

Here is one that is not so obvious. I have created a yellow box part with a script inside it, wrote my code, and saved it. There are no red or blue lines, and then I run the code. Bam! error!


Error - Where to find it

You will notice there is the big glaring red error message, but then below that you will see 3 lines of blue writing.

We want to look at the middle one.


Script ‘Workspace.Part.errorScript1’, Line 4


You get 3 pieces of information to help you find your error.

  1. The file path - Workspace.Part

  2. The file name - errorScript1

  3. The line - Line 4 (Where the error is)

In Roblox Studio, if you move your mouse over the line and click it will open the script and take you to the line where the error is! Great! But what does it mean?


Error - What does it mean?

Ok, now I don’t have any blue or red lines so it must be right, right? Well, not exactly. Some errors are not as obvious as making typing mistakes.

To begin I will explain what I was trying to do in the code, then I will explain why it does not work, and finally I’ll show you the correct way to code it, so it does work.


What was I trying to do?

Ok, the first thing is to think about. “What was the code supposed to do?” Try to put it into words as if you were explaining it to someone. Go through it line by line.


local box = script . Parent

I created a variable called “box” that holds a reference to the part this script is attached to, and all its properties.


box . CanTouch = Instance . new ( “BoolValue" , box )

I create a new boolean value called “CanTouch” and put it in the box.


local inBox = box : GetChildren ( )

I create a new variable called “inBox” and get everything inside the box.


print ( inBox )

I print the things in the variable inBox to the Output Window.


Where Was I Wrong?

Great explanation! Some of it is spot on! However some of your thinking is a bit off. It is easy to get wrapped up in what you “think” is right, but the fact is you need to be like a detective and accept you are wrong, then work out why.


Let’s look at the big red glaring error message for clues.


CanTouch is not a valid member of Part


Roblox Studio is telling you “I have got to line 4, and your asking me to access a thing called ‘CanTouch’ but it doesn’t exist”


Great, but why didn’t Roblox Studio tell me I was writing the wrong code! The reason is that the error is a “Runtime” error, meaning the problem doesn’t happen until you run the game.

How Do I Fix It?

When you type box.CanTouch Roblox studio firstly gets the “box” variable you created. This contains all the properties of the part, and anything else you create as a “child” to the part.


When you type the period “.” Roblox Studio thinks ...

“Ok, they want me to access one of the properties of the box”. Then when we type “CanTouch” Roblox Studio goes “Hang on a minute, there is no such thing, I’m going to throw an error!”


So the problem is that we have not yet added the “CanTouch” value to the part. So let’s fix this line of code.


Enter this:

local CanTouch = Instance . new ( “BoolValue" , box )


To replace this:

box . CanTouch = Instance . new ( “BoolValue” , box )


Can you see the difference? We have now created the new value instead of trying to access it.


Great! I guess we’re all done, right?

Sorry, but although we fixed the big red glaring error, and the code runs it doesn’t do what we want it to do. Errors are not always big glaring messages. Errors can also be through “thinking” our code is doing one thing, but in actual fact it is doing another.


Now the Output Window shows me this….






And my new value is not called “CanTouch” it’s called “Value”!





My code is not doing what I expect!

There is no errors here, but the code does not do what I intended, and this means we need to change our code.


At the moment I am printing “A Table” which is what is in (or assigned to) the variable “inBox”. A table in Roblox Studio can also be called “an Array” in other languages.

This is what I want the code to do….


“I print all the things in the variable inBox to the Output Window.”


local inBox = box : GetChildren ( )


Roblox studio reads this line like this ...

“So you want me to create a new variable called ‘inBox’ then you want me to look in the box, grab everything in the box, and put it in a table.”


First we need to understand what a table is.


This is an example of a variable. It holds 1 piece of data.

local myAge = 23


This is an example of a table

local myDetails = { “myName” , 23 , “myAddress” }


Tables can hold multiple pieces of data, and they do not all have to be the same type. For this reason we need to write more code to look inside the table that is held in our “inBox” variable.


To look at all the items in a table we can use a for loop written as follows.


for i , stuffInBox in pairs ( inBox ) do

print ( i , stuffInBox )

end


Roblox Studio read this as “Ok, you want me to get the table inBox, then starting at the beginning i which equals 1, get each item of the table and put it in the variable stuffInBox, increasing i for each new item until I get to the end”

It’s almost right

We almost have what we intended, however our new boolean we create is called “Value”, and not “CanTouch”.


When we created the value with the following line of code that is all we did. We did not give it a name, so Roblox Studio simply named it “Value”.


local CanTouch = Instance . new( “BoolValue" , box )


Add the following line of code to give our CanTouch value a name.


CanTouch . Name = “CanTouch"


You should find that our output is now correct.

It should be CanTouch...Sorry my code was a little different from my writing ;)

Summary

Errors are not always obvious. Sometimes it our own errors in how we think about solutions, that when applied to code give us unexpected results. Computers are based on a strict logic. For us as human beings we sometimes assume our “version” of logic is the right one, and this can cause great frustration when our code does not produce what we intended.


The key is to “walk away” for awhile, take a break, and then come back to the problem from a new angle. No matter how hard you try to “change” your computers mind it won’t work until you put it in a way only it can understand. Be clear on what you want the code to achieve before you write it, and refer back to your goal if you get stuck.


It is easy to over complicate even the smallest things.


Learn To Code Games In Roblox - Check out my course!

©2019 by brendonross. Proudly created with Wix.com