🔢
Coding🚀 Ages 7-10Beginner 9 min read

Variables and Score in Scratch

A step-by-step primary lesson on variables in Scratch: make a variable, set and change a score, show it on the stage, reset it at the start, and add a high score. With a worked example and quiz.

Key takeaways

  • A variable is a labelled box that stores a value your program can change
  • 'set [score] to 0' puts a value in the box; 'change [score] by 1' adds to it
  • Resetting the score to 0 at the green flag stops old points carrying over
  • Ticking a variable's checkbox shows its value on the stage as a live counter

What is a variable?

When you play a game, something has to remember your score. Every time you catch an apple or dodge an enemy, the number on screen goes up. The thing that remembers that number is called a variable.

A variable is like a labelled box. The label is the variable's name — for example, score. Inside the box is a value, like the number 7. Your code can look inside the box, put a new value in, or change the value. Variables are one of the most important ideas in all of coding. If you would like the bigger picture across all languages, see variables explained. In this lesson we will use one to keep score in Scratch.

If Scratch is new to you, work through getting started with Scratch first, then come back here.

Step 1: Make a variable

  1. In Scratch, click the Variables category (it is orange) in the blocks palette.
  2. Click Make a Variable.
  3. Type the name score and click OK.

A few new blocks appear, and a small counter shows up in the top-left of the stage. That counter is your variable's value, shown live. Right now it says score 0.

Step 2: Set the score to a value

The block set [score] to 0 puts a value into the box, replacing whatever was there. We want the score to start at zero, so drag this script onto your sprite:

when green flag clicked
set [score] to 0

Click the green flag. The counter resets to score 0. This matters more than it might seem: variables remember their value between runs, so if you do not reset it, last game's points carry into the new game. Setting it to 0 at the green flag gives every game a clean start.

Step 3: Change the score during play

Now the fun part — making the score go up. The block change [score] by 1 adds 1 to whatever is in the box. It is different from set:

  • set [score] to 5 makes the score exactly 5, throwing away the old value.
  • change [score] by 5 adds 5 to the current value. If the score was 10, it becomes 15.

Let's add a point whenever you click the sprite:

when this sprite clicked
change [score] by 1

Click the green flag to reset, then click your sprite a few times. Watch the counter climb: 1, 2, 3... You are reading and updating a variable, just like a real game.

Step 4: Show or hide the score on the stage

The live counter on the stage comes from the checkbox next to the variable.

  • In the Variables category, find the score block with a small checkbox beside it.
  • Tick the box to show the counter on the stage. Untick it to hide it.

You can also drag the counter to any corner of the stage. Showing the score lets the player always see how they are doing.

A worked example: a clicking score game

Let's put the pieces together into a tiny complete game. The goal: click an apple as many times as you can, and win when you reach 10.

  1. Add the Apple sprite.
  2. Add this script to the apple:
when green flag clicked
set [score] to 0
forever
  if (score) = 10 then
    say [You win!] for 2 seconds
    stop [all]
  1. Add this second script to the same apple:
when this sprite clicked
change [score] by 1
go to x: (pick random -200 to 200) y: (pick random -150 to 150)

Here is how it works, step by step:

  1. Clicking the green flag runs the first script. set [score] to 0 gives a clean start, and the forever loop begins watching the score.
  2. Each time you click the apple, the second script runs: change [score] by 1 adds a point, then the apple jumps to a random spot so you have to chase it.
  3. The forever loop keeps checking if (score) = 10. As soon as the score hits 10, the apple says "You win!" and stop [all] ends the game.

Two scripts, one variable — and you have a real game with a score and a win condition.

Step 5: Add a high score (challenge level)

Want to remember the best score across many games? Make a second variable called high score. At the end of a game, check whether the score beat it:

if (score) > (high score) then
  set [high score] to (score)

Because high score is not reset at the green flag, it keeps its value between games and remembers your personal best. The score resets each game, but high score does not — that is the trick.

Try it: make it your own

You now know how to keep score. Try one of these challenges:

  • Lose points: add a "bomb" sprite. When clicked, change [score] by -2.
  • Add lives: make a second variable called lives, set it to 3, and change [lives] by -1 for mistakes.
  • Add a timer: use the built-in timer and end the game after 30 seconds instead of at 10 points.
  • Speed it up: make the apple jump faster or smaller as the score climbs, so the game gets harder.

If a variable does not behave, read your blocks in order and check whether you meant set or change. Fixing problems like that is called debugging, and every coder does it. Have fun keeping score! 🔢

Quick quiz

Test yourself and earn XP

What is a variable in Scratch?

What does 'set [score] to 0' do?

What is the difference between 'set [score] to 5' and 'change [score] by 5'?

Why should you 'set [score] to 0' when the green flag is clicked?

How do you show a variable's value on the stage?

FAQ

A variable can hold a number, like a score or a timer, or text, like a player's name. In a game the most common variable is a number that counts points, lives or time.

Add 'set [score] to 0' right under your 'when green flag clicked' block. Variables remember their value between runs, so without a reset, last game's points carry over into the new one.