Learning Goals 3 min
By the end of this lesson you will be able to:
- Tick the right radio button in the Make a Variable dialog — For all sprites for a global variable, For this sprite only for a local variable.
- Predict whether a change [speed v] by (1) on one sprite will also change the same-named variable on another sprite.
- Choose global for things the whole project shares (score, lives, level) and local for things each sprite needs its own copy of (speed, hit-points, direction).
Warm-Up — three racing cars 7 min
Aisyah is building a three-car race at the KL Grand Prix. Each car is a sprite — Car A, Car B, Car C. She wants each one to drive at its own speed.
She makes one variable called speed. On Car A, she sets it to 5. On Car B, she sets it to 8. On Car C, she sets it to 3. Each car has this little script:
when flag clicked
forever
move (speed) steps
end
speed.She clicks the flag. All three cars drive at exactly the same speed. Whichever car's set ran last wins — the other two get overwritten.
Reveal — why this happens
When Aisyah made the variable, she left the default For all sprites ticked. That makes one shared variable that every sprite sees. There aren't three speeds — there's one speed, and the three sprites are stomping on each other's value.
Today's lesson is the radio button she missed: For this sprite only. Tick that, and every sprite gets its own private speed — three cars, three values, three different speeds.
New Concept — scope: where a variable lives 15 min
Every variable in Scratch has a scope — the set of sprites that can see and change it. Scratch gives you two choices, and you pick once, in the Make-Variable dialog. You can't change it later without deleting and remaking the variable.
The dialog you've been clicking past
Open the Variables palette. Click Make a Variable. You see this:
- A text box for the name.
- Two radio buttons: For all sprites (selected by default) and For this sprite only.
- An OK button.
Up to now you've ignored the radio buttons and clicked OK. That's fine for score and high score — there's only one of those in the whole game. But the moment you have more than one sprite that each need their own copy, you have to tick the second button.
For all sprites — the global variable
A global variable is one shared box. Every sprite reads from the same box, every sprite writes to the same box. Change it from any sprite — every other sprite sees the new value instantly.
when flag clicked
set [score v] to (0)
score. Cat earns a point — Stage's score watcher updates. Daniel's other sprite reads score — same value.Globals belong in the orange-block list at the top of the Variables palette on every sprite — they're available everywhere.
For this sprite only — the local variable
A local variable is a private box that belongs to one sprite. If three sprites each tick For this sprite only and name their variable speed, you end up with three completely separate variables that happen to share a name. Changing one doesn't touch the others.
when flag clicked
set [speed v] to (5)
forever
move (speed) steps
end
speed is 5. Car B has its own speed set to 8 in its own script. They never collide.Locals show up in the orange-block list only on the sprite that owns them. Click another sprite — the variable disappears from the palette, because the other sprite can't see it.
How to spot which is which
In the Variables palette:
- A variable name with no prefix is global. Every sprite sees it.
- A variable name that only appears on this sprite's palette is local to it.
- In the
set/changedropdowns, locals are sometimes shown with the sprite's name in brackets, likespeed (Car A), to tell duplicates apart.
Worked Example — Aisyah's three-car race, fixed 12 min
Let's rebuild the warmup race the right way. Eight steps.
speed (a local variable). You script Car A first (see the ✏️ badge), then duplicate it twice. The cat waves them off.Step 1 — Start with one car
New Scratch project. Delete the cat. Add a Car sprite from the library (any car will do). Drag it to the left edge.
Step 2 — Make a local "speed" variable
Open Variables. Click Make a Variable. Name: speed. Tick the second radio button — "For this sprite only". Click OK. A new orange variable appears in the palette.
Step 3 — Set Car A's speed
Build this on Car A:
when flag clicked
set [speed v] to (5)
forever
move (speed) steps
if <touching [edge v] ?> then
stop [this script v]
end
end
Step 4 — Duplicate Car A twice
Right-click Car A in the sprite list → duplicate. Do it again. You now have Car A, Car2, Car3. Each one carries its own private speed variable — currently all set to 5.
Step 5 — Change Car2's speed
Click Car2 in the sprite list. Open its script. Change the set [speed v] to (5) to set [speed v] to (8). Don't touch Car A or Car3.
Step 6 — Change Car3's speed
Click Car3. Change its set to set [speed v] to (3).
Step 7 — Move the cars apart vertically
Drag each car to a different y on the Stage so you can see them race. Car A near the top, Car2 in the middle, Car3 near the bottom.
Step 8 — Click the flag
Car2 (speed 8) wins. Car A (speed 5) comes second. Car3 (speed 3) trails. Three sprites, three private speed values, one race. Because the variable is local, the three values stay separate.
The full assembled stack (on each car)
when flag clicked
set [speed v] to (5)
forever
move (speed) steps
if <touching [edge v] ?> then
stop [this script v]
end
end
set changes. The variable speed is local — three private copies.What you just built: the basic skeleton of any racing or multi-character game. Mario and Luigi have separate hit-points. Each Pong paddle has its own y. Each Pac-Man ghost has its own personality variable. All locals.
Try It Yourself — three scope drills 15 min
Goal: Make a global variable called score (For all sprites). On the cat sprite, add a script that increases it by 1 when the cat is clicked. Add a second sprite (a bowl of kuih). Give the bowl its own click handler that also adds 1 to score. Click both — the same watcher on the Stage goes up.
when this sprite clicked
change [score v] by (1)
score box.Think: If you'd ticked "For this sprite only", the cat would have its own score and the bowl would have a separate score — and the Stage watcher would only show one of them. Globals are how sprites cooperate.
Goal: Build two cat sprites — Cat A and Cat B. Give each one a local hp variable (For this sprite only), both starting at 10. When the space key is pressed, Cat A loses 1 hp. When the up-arrow is pressed, Cat B loses 1 hp. Put the watcher for both hps on the Stage and watch the two numbers diverge.
when flag clicked
set [hp v] to (10)
forever
if <key [space v] pressed?> then
change [hp v] by (-1)
wait (0.3) seconds
end
end
Think: Two watchers on the Stage with the same name. Scratch labels them hp (Cat A) and hp (Cat B) so you can tell them apart — proof that they really are two separate boxes.
Goal: Two sprites — Daniel and Priya — each with a local coins variable starting at 0. Also a global total coins starting at 0. When Daniel is clicked, his local coins goes up by 1 and the global total coins goes up by 1. Same for Priya. You should be able to see the individual counts and the team total all going up at once.
when this sprite clicked
change [coins v] by (1)
change [total coins v] by (1)
change blocks, one local, one global. The dropdown will show both Daniel's local coins and the shared total coins.Think: This is the bread-and-butter pattern for team scoreboards in multiplayer games — per-player stats stored locally on each sprite, summed up into a global total. Real games scale this to dozens of locals.
Mini-Challenge — the duplicated bug 5 min
"Daniel's two ghosts"
Daniel is building a haunted-house game. He builds a ghost sprite with a local variable spookiness set to 5, and a script that floats around the screen at spookiness speed. It works perfectly. He duplicates the ghost twice — Ghost2 and Ghost3 — wanting different spookiness for each.
when flag clicked
set [spookiness v] to (5)
forever
move (spookiness) steps
if on edge, bounce
end
He clicks Ghost2 and changes its set to set [spookiness v] to (10). He clicks Ghost3 and changes its set to set [spookiness v] to (1). He clicks the flag. All three ghosts float at speed 5. What went wrong?
Reveal one valid solution
When Daniel made the original spookiness variable on Ghost, he forgot to tick For this sprite only. So it's a global. All three ghosts share one box. The last script to run its set wins — and because all three hats fire at the same flag, the order is unpredictable. Often it's whichever sprite is highest in the sprite list.
The fix is structural: delete the global spookiness, remake it as "For this sprite only" on the original Ghost, then duplicate. Every duplicate now carries its own private spookiness and the three numbers stay separate.
when flag clicked
set [spookiness v] to (5)
forever
move (spookiness) steps
if on edge, bounce
end
Same blocks. Different scope. Scope isn't visible in the blocks — it's in the variable definition. That's why this bug is so easy to miss.
Recap 3 min
Every variable has a scope — and you pick it once, in the Make-Variable dialog. For all sprites makes a global: one shared box every sprite reads and writes. For this sprite only makes a local: a private box that lives on that one sprite, invisible to the others. Use globals for things the whole game shares (score, lives, level). Use locals for things each sprite needs its own copy of (speed, hit-points, costume-index). Get this choice wrong and your sprites will either trample each other or fail to cooperate — and the bug won't show up in any block, only in the variable's definition.
- Scope
- The set of sprites that can see and change a variable. Scratch supports exactly two scopes: global (every sprite) and local (one sprite).
- Global variable
- A variable made with For all sprites ticked. One shared box. Every sprite reads and writes the same value. Use for
score,lives,level,game over. - Local variable
- A variable made with For this sprite only ticked. A private box owned by one sprite. Other sprites can't see it. Duplicating the sprite duplicates the box. Use for
speed,hp,direction,my colour. - Make a Variable dialog
- The little window with two radio buttons that appears when you click Make a Variable in the Variables palette. The one place scope is chosen — and it's locked once you click OK.
- Sprite list
- The strip of sprite thumbnails in the bottom-right of the Scratch editor. Clicking a sprite switches the palette and code area to that sprite — and shows you that sprite's locals.
Homework 2 min
The Two-Box Quiz Project. Build a project that uses one global and two locals — and label them clearly with watchers on the Stage.
- New project. Delete the cat. Add two sprites — pick any two from the library (Aljay suggests a teh-tarik glass and a kuih lapis).
- Make a global variable called
clicks. Show its watcher on the Stage. - On each sprite, make a local variable called
my clicks. Tick For this sprite only. Show both watchers on the Stage — they'll be labelled with the sprite's name. - On each sprite, build: when this sprite clicked → change [my clicks v] by (1) → change [clicks v] by (1).
- At the top of each sprite, add a when flag clicked → set [my clicks v] to (0). (Reset the local. We'll meet this pattern formally in the next lesson.)
- Click the flag. Click each sprite a different number of times. Watch the two locals diverge and the global keep the running total.
Save as HW-L2-34-Two-Box-Quiz.sb3.
Bring back next class:
- The
.sb3file. - Your answer to: "If you delete the global
clicksand re-create it with the same name but For this sprite only, what changes in the Stage watchers and in the scripts?" (Try it on a copy of the file first — you'll lose script references.)
Heads up for next class: SCR-L02-35 tackles the one job your when ⚑ clicked hat should always do — reset every variable to its starting value, so the second game doesn't inherit ghosts from the first.