Now, I know the title seems very boring and nowhere near as interesting as some of the other topics, but I believe that print debugging can solve quite a lot of problems!
What is print debugging?
Print debugging, according to Wikipedia is…
Print debugging or tracing is the act of watching (live or recorded) trace statements, or print statements, that indicate the flow of execution of a process and the data progression
Now, I don’t know about you but I don’t speak wikipedia. Here’s a slightly easier definition:
Placing print statements to see where code works and where it doesn’t.
That is literally it. Place accurate and useful print statement to check if code is being run as intended.
It’s like making custom errors without throwing an error.
Where should I use print debugging?
Print debugging is best used when a part of code is not running or not running as intended. There are two key aspects to print debugging.
- Where you place the print.
- What you print.
Learning where to place print statements is pretty easy, and learning what to put in them is also quite easy. The more you do it, the easier it becomes.
It’s more useful when there are no errors, since normally you can use errors to debug and find out what causes them.
Can you give me an example of Print Debugging in action?
Of course!
Let’s take a look at my cool new tool. I want it to drop a bomb whenever I click.
Code
local myTool = script.Parent
local myBomb = game:GetService("ReplicatedStorage").myBomb
myTool.Activated:Connect(function()
local myClone = myBomb:Clone()
myClone.BrickColor = BrickColor.new("Lilac")
myClone.Position = myTool.Handle.Position
myClone.Anchored = false
end)
But, oh dear! It doesn’t work! What a shame. Whatever will I do…
Fear not! Print Debugging is here! Let’s first print out myClone
to see if the Activated
event is being triggered.
Code
local myTool = script.Parent
local myBomb = game:GetService("ReplicatedStorage").myBomb
myTool.Activated:Connect(function()
local myClone = myBomb:Clone()
print(myClone)
myClone.BrickColor = BrickColor.new("Lilac")
myClone.Position = myTool.Handle.Position
myClone.Anchored = false
end)
Voila! It prints “myBomb”! So that means it’s definitely being triggered, and so that’s not the problem. It also tells us that myClone
is not nil, and definitely exists. What about some details about myClone
… like its Parent!
Code
local myTool = script.Parent
local myBomb = game:GetService("ReplicatedStorage").myBomb
myTool.Activated:Connect(function()
local myClone = myBomb:Clone()
print(myClone.Parent)
myClone.BrickColor = BrickColor.new("Lilac")
myClone.Position = myTool.Handle.Position
myClone.Anchored = false
end)
Aha! It prints nil
! This is because we need to specify where exactly :Clone()
should place the object. Let’s set myClone
’s parent and see if that does anything…
Code
local myTool = script.Parent
local myBomb = game:GetService("ReplicatedStorage").myBomb
myTool.Activated:Connect(function()
local myClone = myBomb:Clone()
myClone.Parent = workspace
myClone.BrickColor = BrickColor.new("Lilac")
myClone.Position = myTool.Handle.Position
myClone.Anchored = false
end)
Yay! It works now !! Clumsy me!
Now, I know that was a pretty boring demonstration, but it can come in handy quite frequently. For example, I was recently working on a module script in OOP for projectiles. As it happens, quite a few things broke and didn’t work. Print debugging really saved my sanity there, as it allowed me to pinpoint what exactly was or wasn’t working, and fix it without issues.
You’re now a professional print debugger! Happy print debugging! Never underestimate the power of a mere print statement, the first thing you’ve ever learned about programming !
I hope this tutorial helped - it was my first tutorial so any feedback would be greatly appreciated! I know it was pretty short, as it is a relatively small topic, but I hope you at least learned something