JJ_ayx
JJ_ayx

Reputation: 1

lua calling the same function as a loop

I'm making a D&D project, and this is the stats portion. I want to call the stats1 function for rerolling. Can anyone help me?

function stats1()


Strength = math.random(1,20) 
  Dexterity = math.random(1,20) 
  Constitution = math.random(1,20)
  Intelligence = math.random(1,20) 
  Wisdom = math.random(1,20) 
  Charisma = math.random(1,20) 
  print("           Stats            ")
  print("--------------------------")
  print("|  Strength  |     "..Strength.."      |")
  print("--------------------------")
  print("| Dexterity  |     "..Dexterity.."      |")
  print("--------------------------")
  print("|Constitution|     "..Constitution.."      |")
  print("--------------------------")
  print("|Intelligence|     "..Intelligence.."      |")
  print("--------------------------")
  print("|   Wisdom   |     "..Wisdom.."      |")
  print("--------------------------")
  print("|  Charisma  |     "..Charisma.."      |")
  print("--------------------------")
  print("Reroll stats?")
  reroll = io.read
  if reroll == "y" or "Y" then
    for Re_Roll = true
      stats()
      if reroll == "n" or "N" then
  Re_Roll = false
  end
end
  else 
      print("thanks for being cultured")
    end

I'd enjoy the feedback if you can. Thanks!

Upvotes: 0

Views: 108

Answers (1)

Nifim
Nifim

Reputation: 5021

You have a number of mistakes in your function

  1. reroll = io.read. This assigns the function io.read to reroll not the value of a read value. you need to call io.read and set reroll to the resulting value.

    reroll = io.read()
    

    as stated by @EgorSkriptunoff in the comments.

  2. this type of statement will always be true:

    if reroll == "y" or "Y" then
    

    you need to compare both strings to reroll or you will evaluate the truthy value of "Y" which is always true.

  3. for Re_Roll = true will generate an error as it is not a complete for loop statement. it appears you did not intend to use for here so i suggest removing it.

  4. in addition to the issue from item 1, your "N" statement, is nested inside your first if statement, this means it will never be true.

  5. your else statement comes after the end for your if statement this is wrong in Lua and will cause an exception.


Your code does not attempt to make a loop to allow for the reroll to happen. I also suggest reading up on the lua documentation, and I particularly suggest reading Programming in Lua to help get a better understanding of the language.

Here is the code with the noted corrections, and a while loop to allow for rerolls.

function stats()
  Strength = math.random(1, 20) 
  Dexterity = math.random(1, 20) 
  Constitution = math.random(1, 20)
  Intelligence = math.random(1, 20) 
  Wisdom = math.random(1, 20) 
  Charisma = math.random(1, 20)

  print("           Stats            ")
  print("--------------------------")
  print("|  Strength  |     " .. Strength .. "      |")
  print("--------------------------")
  print("| Dexterity  |     " .. Dexterity .. "      |")
  print("--------------------------")
  print("|Constitution|     " .. Constitution .. "      |")
  print("--------------------------")
  print("|Intelligence|     " .. Intelligence .. "      |")
  print("--------------------------")
  print("|   Wisdom   |     "..Wisdom .. "      |")
  print("--------------------------")
  print("|  Charisma  |     " .. Charisma .. "      |")
  print("--------------------------")
end

-- This will run the stat function then ask the user if they want to reroll.
stats()
while(true) do
  print("Reroll stats" .. (Re_Roll and " again" or "") .. "?")
  reroll = io.read()
  if reroll == "y" or reroll == "Y" then
    Re_Roll = true
    stats()
  elseif reroll == "n" or reroll == "N" then
    if not Re_Roll then
      print("thanks for being cultured")
    end
    Re_Roll = false
    break
  end
end

Upvotes: 2

Related Questions