Note: I realise that this will not be useful to most on the dev forum, though I feel like this little series of tutorials I will be making would feel more complete if I included everything.
The next tutorial: Beginner scripting tutorial #2 - Variables
Print statements
Questions we will be answering:
- What are they?
- Why are they useful?
- How do I use them?
Expected knowledge:
- Basic understanding of Roblox Studio and how to insert objects.
What are print statements?
A “print statement” is a piece of code that simply allows you to send a message into the
output
(console). The message can consist of letters, numbers, or symbols, as seen in this image:
Why are print statements useful?
Print statements are useful mainly for debugging. That means finding, and fixing, logical errors in your scripts. This could be done by printing something out whenever X happens. For example, you’ve made a gun, but whenever you click, it doesn’t shoot! You could use a
print statement
to print something out whenever… let’s say, the player clicks the mouse. If the print works, we know that the mouse click was detected! If it doesn’t work, then we know it wasn’t detected. If you didn’t really catch any of that, then don’t worry, knowing whyprint statements
are useful isn’t really a necessity right now, just make sure you know how to use them.
How do I use print statements?
Using
print statements
is surprisingly easy:
- Place a
script
inside of theworkspace
and open it up (double click).
- You should see a screen similar to this (likely with different colours, this can be ignored):
You may notice that there is something in our script already:print("Hello world!")
- now try running the game. You should see a"Hello world!"
pop up in theoutput
. If you didn’t realise already, this is howprint statements
are used; anything inside theprint()
brackets will go directly to theoutput
. One more thing you need to be wary of however, when using prints, if you want to print out letters, you should wrap the sentence in quotation marks (""). But when printing out numbers, you can ignore the quotation marks and put them directly into the brackets:print(5)
.
Practice questions
Try out some practice questions to make sure you really have mastered print statements
:
Question 1 (Easy)
What would appear in the output
if I ran this piece of code?
print("Hello world!")
Hint: Remember, whatever we put inside of the brackets will appear in the output
Answer: Hello world!
Question 2 (Medium)
How would I go about putting the word “Roblox” into the output
?
Hint: Make sure the word “print” is in lower cases and there are speech marks around the word “Roblox”
Answer:
print("Roblox")
Question 3 (Hard)
What would appear in the output
if I ran this piece of code?
print("One" ..1.. " Two" ..2.. " Three" ..3)
Hint: This is something called concatenation. It’s used to connect different values like letters and numbers into one single thing. This will be looked at more in the next tutorial about Variables
.
Answer:
One1 Two2 Three3