Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

TypeError: Cannot read property 'findEnemies' of undefined

Hello!

My soldiers are unable to find enemies. Here is part of my code:

var friends = hero.findFriends();
var person = friends[0];
var enemies = person.findEnemies();

The program highlights the third line (var enemies) and sends me the message
image
I have checked over my code and I’m certain I have no typos.

I am playing the level Kelvintaph Crusader. If you need the rest of the code, let me know!

Thank you in advance :heart:

Also, when I change the code to:

var friends = hero.findFriends();
var person = friends[0];
var enemies = person.findByType("witch")[0];

Then it gives me a similar error message:
image

you never specified if there is actually a friend or not. use this line:

if person
*your code here*

this also happens when i check enemy distances. you have to make sure that the target exists

Hope this helps
Riley

Thank you for replying!

I tried adding an if statement, and it still gives me an error message. Here’s a picture:

oh that… for findByType it can only be used on the hero. other friends dont know that method and therefore cannot use it. since the findByType method returns an array, you only need to use person.findnearest(hero.findByType(type)). which acts as a way to find the nearest enemy of that type

Ohhhh, thank you!! I get it now :smile:

Happy to help :slight_smile: