# Stuck on stillness with motion

**URL:** <https://discourse.codecombat.com/t/stuck-on-stillness-with-motion/5790>\
**Category:** Uncategorized\
**Created:** [November 26, 2015, 12:57am UTC](https://discourse.codecombat.com/t/stuck-on-stillness-with-motion/5790 "2015-11-26T00:57:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KiteMan](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KiteMan](https://discourse.codecombat.com/u/KiteMan)\
**Post date:** [November 26, 2015, 12:57am UTC](https://discourse.codecombat.com/t/stuck-on-stillness-with-motion/5790/1 "2015-11-26T00:57:21Z")

</div>

```python
loop
    enemy = self:findNearestEnemy()
    self:distanceTo(Enemy)
    if distance >5 then self:attack(Enemy)
    else self:moveXY(40, 35)
end

```

it gives me a error saying "end expected to close if at 7:1) at 7:1

iv’e spent roughly 3 hours changing the code hoping it works i couldn’t get it to work.

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [November 26, 2015, 4:24am UTC](https://discourse.codecombat.com/t/stuck-on-stillness-with-motion/5790/2 "2015-11-26T04:24:47Z")

</div>

Hello, KiteMan, and welcome. Please read the FAQ before you post again, in order to learn how to format our code. I’ve done it for you this time, but do so yourself the future.

There are several things wrong with your code. I’ll go through them one by one:

Line 2: You are trying to define a variable `enemy` here, but you need to put `local` before you define anything.

Line 3: Just using `distanceTo` won’t work at all. You need to define a variable that stores the distance.

Line 4: You don’t define `distance` at all. You should to that. Also, you should probably check for `enemy` before you attack it. One last thing: You attack `Enemy`, but you define `enemy`. In Lua, and most other languages, everything is case-sensitive.

Line 6: You need two `end`s, one to end the loop, another to end the if-statement.

---

<div class="post-metadata">

**Author:** ![KiteMan](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KiteMan](https://discourse.codecombat.com/u/KiteMan)\
**Post date:** [November 26, 2015, 5:10am UTC](https://discourse.codecombat.com/t/stuck-on-stillness-with-motion/5790/3 "2015-11-26T05:10:39Z")

</div>

ah thank you for the reply this helped me out a lot  
😁
