Introduction: Variable Blocks in Scratch
Whether you’re just getting started with Scratch programming or looking to take your projects to the next level, understanding variables and lists is crucial. These powerful tools let you store, manage, and manipulate data — making your games and animations smarter, more dynamic, and interactive.
In this beginner-friendly guide inspired by Kodex Academy’s Scratch tutorial video, you’ll learn:
- How to use variables in Scratch
- How to create a variable in Scratch and apply it
- How to create and manage lists in Scratch
- Practical use cases of variable blocks and list blocks
- Tips to make Scratch games more interactive using variables
- Enhancement features and ready-to-use code snippets
What Are Variables in Scratch
A variable in Scratch is a storage container for a single piece of data at a time—it could be a number, a word (text/string), or boolean‑like (true/false) values. Scratch provides built‑in capability to:
- Create a variable
- Set its value
- Change its value
- Show or hide the variable on the screen (monitor it)
- Use the variable’s value in calculations, comparisons, game logic, etc.
There is always a default variable (“my variable”) present, as explained in the transcript.
Scratch variables are usually of two types:
- Global variable (“For all sprites”): accessible by all sprites and the stage
- Local / sprite‑only variable (“For this sprite only”): accessible only within one sprite. en.scratch-wiki
Also, Scratch supports cloud variables, which are global variables stored on Scratch’s servers, sharing state across users or project instances. However, there are limitations (e.g. only numeric, update rates, etc.). en.scratch-wiki
How to Create and Use Variables in Scratch
Based on the Kodex Academy transcript, here are step‑by‑step ways to use variables in Scratch, with added tips and code blocks.
Creating a Variable
- Click the Variables block palette (orange color).
- Choose Make a Variable.
- Type a name (for example,
score
,playerName
,lives
). Make it descriptive. - Choose scope: For all sprites (global) or For this sprite only (local).
- Press OK. The variable appears in the variable palette as well as a checkbox to show/hide it on the stage.
Tip: Use meaningful names (playerScore
, livesLeft
) rather than generic ones—helps when your project gets larger.
Basic Variable Blocks
After creating a variable, some of the most commonly used blocks are:
set [variable] to (value)
— sets the variable to a specific value.change [variable] by (value)
— increases or decreases the variable by a value.show variable [variable]
— displays the variable monitor on stage.hide variable [variable]
— hides the monitor.(variable)
— reports/returns the current value.
Example: Reset Score on Game Start
Suppose you have a game where you want the score
to start at zero each time the game starts. You could script:
when green flag clicked
set [score v] to (0)
Example: Increase Score When Collecting an Object
when this sprite clicked
change [score v] by (1)
Example: Hide / Show Score
when green flag clicked
hide variable [score v]
// Later in game, say when level begins
show variable [score v]
What Are Lists in Scratch
A list is like a variable but can contain multiple items (text, numbers). You can think of it as an array or group of values. Lists are great when you need to manage collections: names, days, levels, scoreboard entries, inventories, etc.
List blocks in Scratch allow you to:
- Create a list
- Add items to it
- Delete items
- Insert at position
- Replace items
- Find the length (number of items)
- Check if list contains a particular item
- Show or hide the list on the stage
Using Lists – Step by Step
Using the transcript’s scenarios, here’s what to do:
- Click Variables palette → Make a List.
- Name it (e.g.
Weekdays
,Inventory
,HighScores
). - The list appears in the palette. You’ll see list‑related blocks appear (add, delete, length, etc.).
Adding Items
when green flag clicked
add [Monday] to [Weekdays v]
add [Tuesday] to [Weekdays v]
add [Wednesday] to [Weekdays v]
Deleting Items
// delete first item
when green flag clicked
delete (1) of [Weekdays v]
// delete all items
delete all of [Weekdays v]
Checking If an Item Exists
contains [Weekdays v] [Monday]
Find Length of the List
length of [Weekdays v]
Hide / Show List
show list [Weekdays v]
hide list [Weekdays v]
Full Example: Making a Simple Game with Variables and Lists
Let’s imagine a quiz game. You want to:
- Keep track of the player’s score
- Keep a list of questions used (so you don’t repeat them)
Here’s a skeleton script for one sprite (“QuizMaster”) and the Stage:
// At start
when green flag clicked
set [score v] to (0)
delete all of [UsedQuestions v]
show variable [score v]
show list [UsedQuestions v]
// Question loop
repeat until <(number of questions answered) = (total questions)>
ask [What is 2 + 2?] and wait
if <(answer) = [4]> then
change [score v] by (1)
say [Correct!] for (2) secs
else
say [Oops!] for (2) secs
end
add [What is 2 + 2?] to [UsedQuestions v]
end
// After quiz
say (join [Your final score is ] (score))
This uses variables (score
) and list (UsedQuestions
) together to build an interactive game.
Additional Scenarios & Use‑Cases
- Timer or Countdown: Use a variable
timeLeft
, decrement every second, show it on screen. - Lives System: Variable
lives
set to some initial number; change by ‑1 when hit, if 0 then game over. - Inventory in Adventure Games: Use list to store items the player collects.
- High Score List: Sort or maintain a list of top scores; use
add
,delete
,length
, etc. - Dynamic names: Use variables to store player name or inputs, then use in dialogues.
Enhancement Features & Advanced Code Ideas
To go beyond basics, here are enhancements you can add, with code templates:
1. Cloud Variables for Leaderboards
Using cloud variables, you can store high scores or global stats that persist across sessions and are visible to others.
// Only numeric values allowed in cloud variables
when green flag clicked
set [cloudHighScore v] to (0) // Cloud variable set in creation dialog
Then you can compare and update if the player beats the previous high score.
2. Dynamic Difficulty Adjustment
Use variables to adjust how hard the game gets:
when green flag clicked
set [score v] to (0)
set [difficulty v] to (1)
forever
wait (5) seconds
change [difficulty v] by (1)
end
// When spawning enemies, use difficulty
spawnEnemy
set speed to (difficulty * 2)
Using Lists for Randomized Content
To avoid repetition, you can pick random items from a list and then remove them to ensure uniqueness:
when green flag clicked
delete all of [Questions v]
add [Q1] to [Questions v]
add [Q2] to [Questions v]
add [Q3] to [Questions v]
// etc.
repeat until <(length of [Questions v]) = (0)>
set [index v] to (pick random (1) to (length of [Questions v]))
set [currentQuestion v] to (item (index) of [Questions v])
// ask currentQuestion etc.
delete (index) of [Questions v]
end
Improved UI / Display
- Show/hide variable monitors depending on game state (intro vs gameplay).
- Use large readout monitors for important variables like timer or lives.
- Use the slider monitor for variables that change within a range (e.g. volume settings etc.).
Animations or Interactive Effects Controlled by Variables
For example: control sprite size, color, speed based on variable value.
when green flag clicked
set [speed v] to (5)
forever
move (speed) steps
wait (0.1) seconds
if <key [up arrow] pressed?> then
change [speed v] by (1)
end
if <key [down arrow] pressed?> then
change [speed v] by (-1)
end
end
Troubleshooting & How to Avoid Errors
- Using vague variable/list names → choose names that reflect purpose (
score
,health
,inventory
). - Forgetting to reset variable or list at start (on green flag) → causes unexpected behavior.
- Showing too many variables or lists on screen → may clutter your stage. Use show/hide appropriately.
- Using wrong scope (sprite vs all sprites) → some sprites won’t access local variables of others.
Conclusion
Mastering variables and lists in Scratch is a game-changer when it comes to creating engaging, interactive, and smart projects. Whether you’re building a Scratch game, developing a quiz, or working on a creative animation, understanding how to create a variable in Scratch or manage a list opens up endless possibilities.
In this tutorial, we’ve covered:
- ✅ What variables are and how they store data like scores, names, or any numeric value.
- ✅ How to use essential variable blocks such as
set
,change by
,show
, andhide
. - ✅ How to create a list in Scratch and use list blocks to add, delete, search, and manage multiple data entries.
- ✅ Practical code examples that help you build smarter logic into your Scratch programs.
- ✅ Enhancement tips to take your projects from beginner to pro level.
Whether you’re just starting or aiming to create advanced games in Scratch, working with variables and lists is the key to unlocking the full potential of Scratch programming.
Call to Action
- Don’t forget to check out the full video tutorial: How to Use Variables in Scratch | Variable Blocks Tutorial in Scratch – Kodex Academy
- Like, comment & share the video
- Visit kodexacademy.com
- subscribe to the Kodex Academy YouTube channel for deeper Scratch content.
Happy coding with Kodex Academy! 🚀
Learn More with Kodex Academy
At Kodex Academy, we’re passionate about helping students learn coding in creative ways. This project teaches more than Scratch—it empowers young minds to build tools that work in the real world.
Explore more:
Stay updated with new content, free tutorials, and coding challenges!
- 🌐 Website: https://kodexacademy.com
- 🌐 Website: https://games.kodexacademy.com
- 💬 WhatsApp Channel: Join Now
- 💼 LinkedIn: Kodex Academy
- 📸 Instagram: @kodex_academy
- 𝕏 Twitter: @Kodex_Academy
- 📢 Telegram: Join Our Channel
- 🔗 Patreon: patreon.com/KodexAcademy
Further Reading & Links
- Scratch Wiki Motion Blocks: https://en.scratch-wiki.info/wiki/Motion_Blocks
- Scratch Programming for Beginners: https://scratch.mit.edu/projects/editor
- Scratch Animation Guide: https://en.scratch-wiki.info/wiki/Animating