Reputation: 1
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?
Upvotes: 0
Views: 1009
Reputation: 2147
right idea, just tweak it a little so you aren't using the same variables
io.write("Hello, what is your first player's name? ")
local name1 = io.read()
io.write('How old is Player 1? ')
local age1 = io.read()
function Recall( name, age )
print("Your player's name: ", name, "\nYour player's age: ", age, "\n")
end
Recall( name1, age1 )
io.write("What is your second player's name? ")
local name2 = io.read()
io.write('How old is Player 2? ')
local age2 = io.read()
Recall( name2, age2 )
Upvotes: 1
Reputation: 1752
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.
Upvotes: 2