Debugging your first program

You will learn how to identify issues with your code, and then fix them

by NotAPorgu

Author Avatar


Welcome to my tutorial on how to debug your program! In this tutorial I'll teach you to diagnose your naughty program by showing you where to start looking, what to look for, and how to apply solutions.

It should be obvious that I can't solve your specific issue, but hopefully the examples I give can help you have a better mindset when approaching your program. I will start by giving an example of a problem someone was having with their code, and how that problem was identified.

Step One: Check for errors

A lot of times when people ask for help with their faulty code they will say something along the lines of "here's my code, why isn't it working?" To which I always respond with "Was there an error message?"

The first place to look is in your output window. If you run your script and there is an error message, keep reading and I'll tell you how to read it. If you don't have an error message, and the script isn't doing what it's supposed to, proceed to the next step.

Since there was an actual error in your code, it will output something along the lines of this:

ServerScriptService.Tutorial:1: attempt to compare number with boolean Stack Begin Script 'ServerScriptService.Tutorial', Line 1 Stack End

This is a rather simple example, but let's analyze it. The first line us it's in ServerScriptService, and it's in a script named "Tutorial". It also tells us what exactly went wrong, in this case:

attempt to compare number with boolean

We are also given a location within the script from where the error occured:

Script 'ServerScriptService.Tutorial', Line 1

The most important item we need this is the line number, which is 1. Now let's look at the script that gave this error:

if true > 10 then

Now hopefully it should be obvious that comparing 'true' with a number value doesn't make sense, and you wouldn't intentionally do this, but this could happen if 'true' and or '10' were stored in a variable elsewhere in the script, like so.

if value1 > 10 then

Assuming that value1 was given a true/false value elsewhere in the script, you would recieve the same error.

Sometimes you'll have more in the error message then just one line, so let's see what that would look like:

ServerScriptService.Tutorial:2: attempt to compare number with boolean Stack Begin Script 'ServerScriptService.Tutorial', Line 2 - local testing Script 'ServerScriptService.Tutorial', Line 6 Stack End

As you can see, there's another line mentioned in the stack. Before I tell you what's different, let me show you what the script looks like:

local function testing(value)
    if value > 10 then

This time, the error is on line 2, with an additional message about line 6. But what's that about, line 6 is just calling the function right? Exactly. In this case, the function is being passed a value that isn't compatible with what it's being used for, and knowing where the function was called can help you determine why it was fed the wrong value type. For larger scripts, the stack might have a dozen or so calls in it, which can help you trace back the values being passed, if that's what is causing the problem.

Though I will say, in most cases you will only need to look at the line mentioned at the top of the stack, in this case line 2.

There are dozens of error messages, so I can't cover them all in this lesson, but generally the error's output message, along side the line number for where the error took place, you should be able to figure out what the problem is. If the reason for error doesn't make sense, or you're not sure why that error is taking place, Google is your friend. Simply copy the error and paste it into the search bar, add "roblox" to the end of it, and start looking through the results.

Step Two: Identify the problem

So you've gotten your script to the point where it's "working", or at least isn't erroring, but it isn't doing what you were expecting. No worries, with a little guidance you can figure it out!

This person (for this tutorial's sake, let's call him Jeff) was trying to create a gui that appeared above the player with a number on it. Having to prior experience with GUIs, I took the script he gave, and recreated what he had in studio. I examined the code, and saw that it placed a GUI into the player's character, so I looked at the GUI in explorer and found 2 things. 1st, the GUI was never activated 2nd, it was never given a size This was all of the information Jeff needed to know in order to fix his script and get it working.

My point in telling you this is that you should first figure out what your script is doing, then figure out what you need to change in order to make it do what it's supposed to do.

Sometimes you might figure out the issue, but not know how to solve it. That brings us to the next step.

Step Three: Double check what you know

If you're using objects that you're not familiar with, (for me it was CFrames for a while) then that might be contributing to why your script isn't working. Double check what you're unsure about on the developer wiki to make sure you're using everything correctly. If the wiki is too vague, and doesn't give enough information to tell you what is wrong, you can look for more information through Google.

If you don't find enough information to help you, try searching for something similar to what you're trying to do. Be it bullet drop for an fps, or a random map generator, you're likely to find something similar to what you're doing online. Once you find something, compare how you're doing it to how they are doing it and look for anything different.

Step Four: Ask the Lua Learning community!

If you can't get past step three, there is a rather large community that surrounds lua learning, and we're always willing to help! The discord can be in the social links for Lua Learning.

In order to have the best experience, it would be best to supply us with the following information:

1.The part of the script that isn't working What the script is supposed to do What it's actually doing (remeber step 2 of this tutorial!) Any error messages that are appearing

Thank you for taking the time to read this tutorial! I really hope this helped you, good luck scripting!

View in-game to comment, award, and more!