[SOLVED] HELP! Decoy Drill

i have no idea why this code is not working because it seems to be placing nonstop decoys

here is my code


> // We are field testing a new battle unit: the decoy.
// Build 4 decoys, then report the total to Naria.

var decoysBuilt = 0;
while(true) {
    var coin = hero.findNearestItem();
    if(coin) {
        // Collect the coin!
        hero.moveXY(coin.pos.x, coin.pos.y);
    }
    // Each decoy costs 25 gold.
    // If hero.gold is greater than or equal to 25:
 if   (hero.gold >= 25) {

        // buildXY a "decoy"
            hero.buildXY("decoy",hero.pos.x, hero.pos.y);
}
        // Add 1 to the decoysBuilt count.
        var decoysBuilt = 1;
     
    if(decoysBuilt == 4) {
        // Break out of the loop when you have built 4.
  break;
    }
}

hero.say("Done building decoys!");
hero.moveXY(14, 36);
// Say how many decoys you built.
hero.say("4");

Hello, welcome to the forum.

You aren’t quite following the comments on this one line.

        // Add 1 to the decoysBuilt count.
        var decoysBuilt = 1;

To debug this and see how many decoys you have built, you could add a hero.say(decoysBuilt); right after this section. It helps understand what is happening with your code. Currently, you will see that you only build 1 after every loop.

1 Like

But what do i do then?

You are currently reassigning the variable, decoysBuilt to 1 after every iteration of the loop. Regardless of how many times the code iterates through the loop, decoysBuilt is assigned a value of 1.

// Add 1 to the decoysBuilt count.

This tells you what to do next. Currently, you are assigning a value of 1 when you should be adding 1 (incrementing).

   var decoysBuild =+ 1;

like this?

i solved it
how do i close this

You don’t need to close it, other people often have the same problems and can search the discourse for people stuck on the same level.
Instead you can put a :[SOLVED] in the title. I’ll do it now because I’m here.
-Danny

ok thx
(20charrrsss)

1 Like