Unit 6 Lesson 6 Level 16 Motorcycle game help- re-declaring variable


Unit 6 Lesson 6 Puzzle 16

OK, I don’t know if this is a bug or something I just don’t understand.
I’ve copied my code for the program below.(and you can see my remix at: https://studio.code.org/projects/applab/xb-XonRg-8lspcN-W4C-dJs7hxMlwSLWxJYQfkahT4A

The problem I have is with line 16:
var speed = getProperty(“slider1”, “value”);

The program will not work as is. I have to remove the var to get it to work. Now I understand that it is not good practice to re-declare a variable, but shouldn’t it still work? Wouldn’t it just (non-tech talk) make a new speed variable in place of the old one with the value of the slider? If not, how do the onboard events work when they redeclare a variable each time a button is pressed? I also don’t remember having these troubles when my students did this lesson in the past which is why I’m wondering if this is a bug

This is a copy of the code

// Set the speed of the motorcycle
var speed = 10;

// Move the motorcycle up when the left button is clicked
onBoardEvent( buttonL, “press”, function(event) {
var y = getProperty(“motorcycle”, “y”);
setProperty(“motorcycle”, “y”, y - speed);

// Move the motorcycle down when right button is clicked
onBoardEvent( buttonR, “press”, function(event) {
var y = getProperty(“motorcycle”, “y”);
setProperty(“motorcycle”, “y”, y + speed);
onEvent(“slider1”, “change”, function(event) {
var speed = getProperty(“slider1”, “value”);


On line 16 you are creating a new variable named speed which is local to the onBoardEvent() callback function. This new variable is available only within the enclosing function definition. The value will not affect the variable speed declared on line 2. To fix the code, try this on line 16
speed =getProperty(“slider1”, "value);
This will update the original variable speed that was declared on line 2.


@cpautler Just checking in to see if this solution worked for you. Please post a link to the code if you would like further help debugging.


Thank you so much for this. I guess I was somewhat confused about variables as I assumed any variable would work throughout the program. I was wondering why speed wouldn’t change when I used the watcher. I really appreciate the explanation as I have so many kids having the same issue and now I actually have something to tell them!

Much appreciated!

1 Like

You are welcome. Glad to help. :slight_smile:


Can I see what the blocks look like? I did not see an example for this level 16, so I am fearing that the students are going to ask me how to do it. I have no clue. I see the code you put ion the reply, but not sure as to what blocks to actually put. Can I get a link to see the blocks used?

Laura VH


If you click on the link at the top of my post you can see the code. It will work if the var is removed from line 16