# \[Solved\] How do you command properly

**URL:** <https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651>\
**Category:** Level Help\
**Created:** [November 24, 2020, 1:07pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651 "2020-11-24T13:07:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 24, 2020, 1:07pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/1 "2020-11-24T13:07:26Z")

</div>

I can’t seem to figure out how to command my troops to attack this is my code:def commandSoldiers():  
enemies = hero.findEnemies()  
enemy = hero.findNearest(enemies)  
soldiers = hero.findFriends()  
soldier = soldiers[0]  
for soldier in soldiers:

```
    hero.command(soldier, "attack", enemy)

```

It always does this

 ![Screen Shot 2020-11-24 at 8.05.56 AM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/6/b/6ba87a76de0a42f9d29a9ac6ca8051bd51f74b8f.jpeg)

---

<div class="post-metadata">

**Author:** ![riticmaster9087](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/riticmaster9087/32/15515_2.png) [@riticmaster9087](https://discourse.codecombat.com/u/riticmaster9087)\
**Post date:** [November 24, 2020, 1:09pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/2 "2020-11-24T13:09:07Z")

</div>

it is because a peasant is the nearest friend try using findByType(minnion name)

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 24, 2020, 1:11pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/3 "2020-11-24T13:11:50Z")

</div>

So I have to find the soldier I get it

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [November 24, 2020, 3:16pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/4 "2020-11-24T15:16:13Z")

</div>

I actually like using for-loops better than all those indexes.  
For-loops are nice and simple,  
You just have to define friends and then you can command by using a for-loop!  
Ex:

```python
while True:
     friends = hero.findFriends()
     for friend in friends: 
          #command your friends

```

It is nice and simple!  
Lydia

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 24, 2020, 3:20pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/5 "2020-11-24T15:20:23Z")

</div>

okay still getting cozy with for-loops but I like while loops

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [November 24, 2020, 5:50pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/6 "2020-11-24T17:50:41Z")

</div>

You could also just add an if loop identifying all soldiers.

```python
if soldier.type == "soldier":

```

That way, all friends that are soldier types follow that command.

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 24, 2020, 5:56pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/7 "2020-11-24T17:56:40Z")

</div>

I got it @abc this is really helping me out command my pesky soldiers

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [November 24, 2020, 7:26pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/8 "2020-11-24T19:26:44Z")

</div>

> [@riticmaster9087](#):
>
> try using findByType(minnion name)

I agree with @riticmaster9087 on this one. The findByType() method works very well if you have the correct glasses that include this method. One note for this method, you need to understand that this is going to return an array list of all the soldiers. So using a for loop to command them individually works very well.

You can also include an additional parameter that ensures you are only finding friendly soldiers or whatever type you search for. This is important when you fight against warlocks since they can raise your soldiers from the dead to fight against you. If you don’t include the friends limitation, you will try to command the raised soldiers that are now enemies and you can’t, you will get an error.

`hero.findByType("soldier", hero.findFriends())`  
 ![findByType](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/6/4/64d35ed670fb30848c169d0153593098f8eef688.jpeg)

Otherwise, using a for loop with your friends like @Lydia_Song suggested with the type check included like @abc mentioned is another great way to isolate the type of friends you want to command.

Extra note for those that are new to commanding friends: you have the right Boss Star that allows you command the different friends.

> [@Summonable units and their basic stats](https://discourse.codecombat.com/t/summonable-units-and-their-basic-stats/18501):
>
> As I like to have an easily view-able list of sorts for the summon-able units, I thought I’d do this. In case of misspellings, wrong stats or similar, let me know! Hope someone else might find it as interesting or useful as me! Not sure if anyone else might have a need for it though. Summonable Units - as of the 31th of May, 2019 Some basic Summonable-Unit Stats that I thought would be useful in this kind of fashion. (if not changed by level) Soldier - Boss Star I
> 
> > **Click to show/hide Sta…**
> >

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 24, 2020, 7:31pm UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/9 "2020-11-24T19:31:45Z")

</div>

thanks @brooksy125, @Lydia_Song, @abc, and @riticmaster9087 you guys have been a big help now I can tell my troops to serve me fried munchkins

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [November 25, 2020, 7:31am UTC](https://discourse.codecombat.com/t/solved-how-do-you-command-properly/25651/10 "2020-11-25T07:31:45Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
