Search code examples
functionvariablesluavar

How can I change the original variable in lua


I want to change the original variable so the functions prints a different answer. I'm new to Lua, is this possible and how can I do it?

Here's my code:

io.write("Hello, what is your first player's name? ")
local name = io.read()
io.write('How old is Player 1? ')
local age = io.read()

function Recall()
    print("Your player's name: " ,name, "\nYour player's age: " ,age, "\n")
end

Recall()

io.write("What is your second player's name? ")
local name = io.read()
io.write('How old is Player 2? ')
local age = io.read()

Recall()

When I call the function the second time, it prints the name and age in the first input. Any suggestions?


Solution

  • You created new locals and lose the access to locals with the same names.

    To fix that remove local near second name and second age. (Near Player2).

    As alternative solution you can make parameters to Recall function and pass arguments in it.