# Not sure what to do with this error. -lua Language

**URL:** <https://discourse.codecombat.com/t/not-sure-what-to-do-with-this-error-lua-language/5233>\
**Category:** Uncategorized\
**Created:** [October 13, 2015, 5:20pm UTC](https://discourse.codecombat.com/t/not-sure-what-to-do-with-this-error-lua-language/5233 "2015-10-13T17:20:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel\_Metcalf](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Samuel\_Metcalf](https://discourse.codecombat.com/u/Samuel_Metcalf)\
**Post date:** [October 13, 2015, 5:20pm UTC](https://discourse.codecombat.com/t/not-sure-what-to-do-with-this-error-lua-language/5233/1 "2015-10-13T17:20:43Z")

</div>

```python
-- Use "if" and "else if" to handle any situation.
-- Put it all together to defeat enemies and pick up coins!
-- Make sure you bought great armor from the item shop! 400 health recommended.

loop
    local flag = self:findFlag()
    local enemy = self:findNearestEnemy()
    local item = self:findNearestItem()
    local pos = item.pos
        local x = pos.x
        local y = pos.y
    if flag then
        -- What happens when I find a flag?
        self:pickUpFlag(flag)
    elseif enemy then
        -- What happens when I find an enemy?
        if self:isReady("cleave") then
            self:cleave(enemy)
            else
                self:attack(enemy)
            end
            end
    elseif item then
        self:moveXY(pos.x, pos.y)
        -- What happens when I find an item?
        end
    end
end

```

Error: end expected (to close if at 23:5) at 23:5)

Why is this and how can I fix it? Nothing I know I can do seems to work.

I’m willing to try and reply as complete as I can to questions.

---

<div class="post-metadata">

**Author:** ![Samuel\_Metcalf](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Samuel\_Metcalf](https://discourse.codecombat.com/u/Samuel_Metcalf)\
**Post date:** [October 13, 2015, 7:26pm UTC](https://discourse.codecombat.com/t/not-sure-what-to-do-with-this-error-lua-language/5233/2 "2015-10-13T19:26:32Z")

</div>

When in doubt, call @nick

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [October 13, 2015, 7:39pm UTC](https://discourse.codecombat.com/t/not-sure-what-to-do-with-this-error-lua-language/5233/3 "2015-10-13T19:39:38Z")

</div>

You have an extra `end` right before your `elseif item then` line, because you indented the preceding `else` one level too deeply. Try making your indentation perfect and it’s easy to see when things aren’t matched like this. Hope this helps!

---

<div class="post-metadata">

**Author:** ![Samuel\_Metcalf](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Samuel\_Metcalf](https://discourse.codecombat.com/u/Samuel_Metcalf)\
**Post date:** [October 13, 2015, 7:46pm UTC](https://discourse.codecombat.com/t/not-sure-what-to-do-with-this-error-lua-language/5233/4 "2015-10-13T19:46:05Z")

</div>

This helped, thank you very much!
