Stuck on stillness with motion

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.

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 ends, one to end the loop, another to end the if-statement.

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