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.