How to Debug Your Scripts in 2021 - A Basic Tutorial

A Basic Guide To Debugging Your Scripts!

INTRO!

Hello! Welcome! This tutorial is aimed at all scripters, beginners to advanced! This tutorial will help you understand in detail how to properly debug your scripts and will hopefully help you in the future. Let’s dive into it.


PRINTING, ERRORING AND WARNING!

Utilizing print("") and warn("") is a great way to see where the problems are in your script, by periodically adding print statements in your scripts you can easily determine where your script is running into a problem. For example:

print("Script ran!")

local EpicDataStore = game:GetService("DataStoreService"):GetDataStore("EpicDataStore")
local EpicVariable = "Doge"

print("Variables defined!")

game.Players.PlayerAdded:Connect(function(player)
  print(tostring(player.Name).." joined the game!")
  
  local SuperEpicVariable = Instance.new("StringValue")
  SuperEpicVariable.Parent = player

  print("SuperEpicVariable Made!")
  
  local success, errormessage = pcall(function()
    print("Pcall Ran!")
    SuperEpicVariable.Value = EpicDataStore:GetAsync(player)
  end

  if success then
    print("Successfully retrieved data!")
  elseif errormessage then
    warn(errormessage)
  end

  print("Made it to the end!")
end)

As you can see here, I am periodically adding print statements to make sure I know how far the script is getting before erroring so it is easier to find the issue. As you can see, I also used warn("") to send the errormessage as a warning! FAQ: What’s the difference between printing & warning? Answer: Printing prints in normal text, erroring prints in orange text so it is easier to see! You can also use error("") to print in red text, this also breaks the entire script!

Utilizing Errors!

Using the errors in the Output window is vital for debugging. Roblox gives lots of helpful information here, such as the line where the script errored, and an error message!

Here is an example:

As you can see in the example, it says Players.RalphDevv.PlayerGui.charToStage:34: Expected ')' (to close '(' at line 33), got 'end'. This means that in the PlayerGui (StarterGui), in the charToStageScript, on line 33 Lua expected there to be a closing bracket but there wasn’t one, and it got end. And as you can see here in the script:

image

There was no closing bracket, on line 33, where there should’ve been, and what followed was end! See how simple the error messages actually are!? You get all the info you need, all you need to know is how to read it properly. All I needed to do was add a closing bracket! Isn’t that simple!

CONCLUSION!

Right then, we have made it to the end of this tutorial! I hope this was in-depth enough for you all! If you would like anything else to be added feel free to leave a reply! Thanks for reading!

14 Likes

You should’ve probably mentioned breakpoints or the built in lua debugger at least once as well.

6 Likes

Good point, well made.

I will think about adding it!

Yeah this should include breakpoints and the other build in debugging features, like watching variables.
It’s so much more powerful than using print and warn, if used properly.

But all this is already written on the Developer Portal here: Debugging Tools (roblox.com)

using breakpoints ain’t advanced
literally every beginner can learn how to use them quickly

Some people simply don’t like breakpoints.

1 Like