Debugging|Breaking Scripts

INTRODUCTION

We all know debugging is painful and especially when there’s no error to give information out. Don’t you fear because exp_lol123 is going to show you how to debug and even break an entire script!

DEBUGGING

PRINT()

Print, just like warn() too is very useful for debugging. Let’s say your script has stopped working and you don’t know what the problem is. Simply, add a print command on certain lines such as events. Here’s an example:

    script.Parent.Touched:Connect(function(hit)
         print("Starting up") -- this detects if the script has started up.
 if hit.Name == "Cubes"  then --I spelt Cube wrong 
         print("Oh my goodness!") -- tests if cubes(the incorrect part name) has hit it.
       end
 end)

Huh, the script problem is after the print statement. Oh I know why it’s not firing! I spelt Cube wrong!

There’s obviously other problems that can occur while scripting.

Print, in simplest terms basically is an essential to finding root problems and their location since sometimes, the script may not error so the location is not given. Here’s another example:

for i,v in pairs(workspace:GetChildren()) do
print("Looking through workspace")
	if v:IsA("Union") then
		print("It's a union! Yes, \"Union\" is a union!")
		end
	end

This didn’t work because Union is supposed to be a UnionOperation, not Union. This will display no errors as far as I’m aware.
This will, again be able to find the location of the problem.

ERRORS

We’ve found out how to find location of errors but how about, if there’s an error. Errors are normally spoken in English so it’s pretty easy to understand. Also, if there’s an underline, please hover over it so you are prepared for the error. This section will cover a few of the errors, there’s a lot more errors(and I mean hundreds more) to cover. This will just help you get the hang of it.

Let’s start with the most common, yeah we’re talking about the infamous end error.

End Error

What the end error is

The end error, in my terms is basically when the script is missing an end. When this does happen, the script will break. This can also happen when the script has too much ends.
Here’s the error:

Error

FIRST FORM


SECOND FORM

Code
game.Players.PlayerAdded:Connect(function(player)
	if player.Name == "exp_lol123" then
		print("This player is stupid!")
end

Now each statement or event requires an end or end). Events require end) and statements require end. Now count the amount of statements and events:

image

Now count the amount of end/end):

image

Count the amounts of events and end)
1 Event and no end)

Attempt to index as nil error

Let’s now talk about the nil error. The nil error in my terms is simply when something has been identified as nil when it’s not supposed to be nil. The error states that it attempts to index a value/object or anything as nil.

Well, IsA is nil. I wonder why?

    local function wow(obj)
    	if obj:IsA("Part") then
    		print("Object is a part!")
    	end
    end

wow()

Now, I’ll leave you to figure out.

What was wrong

This is a mistake I, when you forget to pass an argument. Object is nil so IsA can’t be a member of nil so it’s basically nil.

Here’s another demonstration:

Normally this will appear if you mistype a variable:

Error

image

This is basically like recommendations. Also a blue underline will appear.
When you hover your mouse over it, it’ll come up with this:

image

Always check and hover over if there’s a underline.
Now, I added a .Name to my code.
Here’s your expected output and your code(totally not trying to replicate what the developer hub do):

Expected Output

Code
   local obj = workspace.Part 
    print(objs.Name)
Invalid Member Error

Now, last but not least, invalid member error!
Valid member error is basically when something is not a valid member of something. Here’s a scenario I made up:

image

It’ll put a blue underline.

Error

After looking at the error, you’ll immediately know what’s wrong. I’ll leave you to figure out.
What’s wrong?

What was wrong

What was wrong was the fact the script was put in the wrong place, it was meant to be put in a part but I put it in ServerScriptService

Here’s another scenario:

Code
local part = workspace.Part
print(part.Name)

Huh, why is this script incorrect?

Error

image

I’ll again leave it to you to figure out, this one is a little tricky though.

This is an error that doesn’t occur all the time but occurs occasionally. This error happens when Part is called before Part has even loaded and hence, it’ll cause this error.

Another scenario:

Code
  local part = workspace.Parss

    print(part.Name)
Error

image

I’ll leave you to figure out.

What was wrong

Yes, that’s correct, I misspelt Part

Let’s now continue on onto warn(). We’ve finished with errors.

WARN()

Warn() is used to notify that something isn’t working right. It’s yellow text like this:

Example

Let’s say you have an if statement like this:

if data == nil  then
warn("Data was lost unfortunately.")
else
print("Sweet, data was not lost!")
end

The warn part will notify something is wrong to the developer. This helps a ton since it gives you instructions on what to fix. Now, I’ve done the print thing already so this section was pretty short. Moving on

BREAKING A WHOLE SCRIPT

Error()

This is used to break an entire script. Let’s say a tool hasn’t loaded, something like this:

Code
local handle = script.Parent.Handle

if handle == nil then
error("Tool hasn't loaded in")
end

Anything after that will not function anymore since your erroring a script, furthermore, here’s what would happen if you tried.

Image 1

Image 2

Image 3(Output)

Why didn’t it print warn(), you’ll probably know why immediately by Image2.

What was wrong

By these images, you could see the script was trying to warn you the warn thing wouldn’t work and it’d not function. You could see the error but you can’t see the warn thing. Errors are able to break scripts and your able to add a string value in them.

Error is useful if you want to break a script. Let’s maybe now do an account termination immunity from that game script with the use of error().

Image1

image
You can see the blue underline.

Image2

10:23:30.850 ServerScriptService.Script:3: He copied your topic, you hadn't even released it yet. - Server - Script:3

Image3

Code
game.Players.PlayerAdded:Connect(function(player)
	if player.Name == "exp_lol123" then
		error("He copied your topic, you hadn't even released it yet.")
		player:Kick("WHYY")
	end
end)

I wonder why he didn’t get banned.

Answer

He didn’t get banned because we errored and broke the script.

Also, use the The Debugger - Enhanced Watch Window if you want to debug like a proper master.

Did you find this topic useful?
  • Yes, I learnt a lot
  • I already knew this stuff but I think it’d be useful for others
  • No, I did not find this useful and I learnt nothing from this.
  • A little bit, not 100% useful

0 voters

Exiting the topic

Right, were finished with this topic! Now, yes I did use a lot of the hide details thing. I used it because the topic would be REALLY long without it. It also organized my topic. And yes, I did aim for a similar layout to the developer hub too so that’s why. Right, I hope this taught you how to debug and break a script! Again(just like in all of my community tutorials) if there is anything wrong, please state it in the replies. And as always, debug out!

6 Likes