Random RPG Battle System in Lua This is my battle system that I wrote in Lua. Bear with me on this one, this is my first real program that does something besides write hello world. If there is anything that I could have done differently, drop an answer down there and I will take your advice for my next program. Huge shoutout to Jakuje for helping me make this, he refined my code for me and told me how to do things correctly.

math.randomseed( os.time() )
math.random(); math.random(); math.random()
local function battle() 
  n = math.random(10) + 1 -- Everybody's HP, enemy HP randomly generated number from 10 to 100
  enemyhp = 10*n
  exp = enemyhp*0.5
  g_herohp = 100
  print("You have encountered an enemy!")
  io.write("Your HP: ")
  io.write(g_herohp)
  io.write(" ")
  io.flush()
  io.write("Enemy HP: ")
  io.write(enemyhp)
  io.write(" ")
  io.flush()
end
local function attack()  -- Attacking the enemy or running away
  print("|Attack|Flee| ")
  input = io.read()
  if input == "attack" then
    attackdamage = math.random(51)
    if attackdamage == 51 then
      print("Critical Hit! 100 damage!")
      enemyhp = enemyhp - 100
    else
      enemyhp = enemyhp - attackdamage
      io.write("You dealt ")
      io.write(attackdamage)
      io.write(" damage!")
    end
  elseif input == "flee" then
    escapechance = math.random(2)
    if escapechance == 1 then
    escape = true
    end
  end
end
local function enemyattack() -- Enemy attacks you with a random amount of damage
  print(" Enemy is attacking!")
  eattackdamage = math.random(40)
  g_herohp = g_herohp - eattackdamage
  io.write("Enemy dealt ")
  io.write(eattackdamage)
  io.write(" damage!")
end
local function battleinprogress() -- If I used battle then it would reset everyones health.
  if g_herohp &lt;= 0 then
    print(" You have died!")
    end
  io.write(" Your HP: ")
  io.write(g_herohp)
  io.write(" ")
  io.flush()
  io.write("Enemy HP: ")
  io.write(enemyhp)
  io.write(" ")
  io.flush()
  if enemyhp &lt;= 0 then
    print("You won!")
  end
  if escape == true then
  print("You have run away!")
  end
end
battle()
repeat
attack()
enemyattack()
battleinprogress()
until g_herohp &lt;= 0 or enemyhp &lt;= 0 or escape == true
end
Since you are making calls to math.random a lot, keep a local copy in your code:

local random = math.random




Why are you switching between io.write and print statements? Use a string.format to format the data as you want:

print( ("You dealt %d damage!"):format(attackdamage) )


The above is merely a representation. It can be made a lot better (by putting templates as variables etc.)



n = math.random(10) + 1


The above will generate n in the range of 2 to 11. From the docs:


  When called with two integers m and n, math.random returns a
  pseudo-random integer with uniform distribution in the range [m,n].
  [...] The call math.random(n) is equivalent to math.random(1,n).


And, no need for keeping a temporary variable (here n):

enemyhp = 10 * random(10) -- better yet, use random(10, 100)




escapechance = math.random(2)
if escapechance == 1 then
escape = true
end


can simply become:

escape = random(2) == 1




Convert your input to lowercase first. Players might enter Attack or ATTACK too.

if input:lower() == "attack"


or

local input = io.read():lower()




Most important

You should encapsulate your player, enemies etc. inside objects. Lua supports a rough implementation of OOP design using tables. Read the Lua PiL chapter #16