Lua combat error

I was trying to go back through the levels and try the lua programing. It pops up with an error saying enemy not found please use enemy = hero:findNearestEnemy(). here’s my code:

while true do
enemy = hero:findNearestEnemy()
hero:attack(enemy)
end

Anyone know why its telling me this error if i have the code it tells me to use?

2 Likes

Try using something like:
if enemy
or
while enemy

2 Likes

Ya i tryed that but then it either wont work or only kill one then stop

2 Likes

What level are you playing?
What gear are you using?
Has this ever happened more than once?

2 Likes

I think that the end is making it stop the loop so it only attacks once.

2 Likes

I just started using lua and i’m replaying the beginning. no this has never happened before. i’m using the some new gear i got and some old ones.

2 Likes

I removed the end but it tells me to fix my code and that i need end.

2 Likes

So is it Dungeon, Forest, Desert, Mountian, or Glacier?
What name is the Level?
The reason I am Asking is to make sure that code combat is working correctly.
So could you send me the link?

2 Likes

I’m in the dungeon on the level mayhem of munchkins.
link to the level: https://codecombat.com/play/level/a-mayhem-of-munchkins

2 Likes

It’s not an enemy its a local enemy.

write instead

while true do
    local enemy = hero:findNearestEnemy()
    hero:attack(enemy)
end
2 Likes

ok thanks. ill try that and see how it works.

2 Likes

now its telling me it received null and asked if hero should always attack and use if. i tried but it wouldn’t work. is there a way i should type an if statement in lua that i don’t know?
(never mind. it works fine now) (thanks)

2 Likes