Keep getting Nil during if then statements Please help!

Hello,

  My name is Kes and I’ve recently started watching tutorials on how to create apps with Corona and I think the package is awesome so far.  I’m very new to coding anything as I’m more of an Concept art assets guy.  I’ve been picking up Lua pretty well so far with functions, tables etc. however I’m getting stuck on this if then statement,

Seems not matter what I do I always get “Nil” as my answer for age instead of the actual number.  When I copy the tutorial’s code it works perfectly.  Yet when I insert my own code it doesn’t seem to work even though it seems like I’m following the formula perfectly.  I’d appreciate some fresh eyes to take a look at my code and help me figure out where I’m going wrong so that I end up with “nil” constantly.  

Thank’s in advance for the help! 

Here’s my code:

local zombie1= {name="Bob", age="10"}local zombie2= {name="Sam", age="20"} local Creatures= {zombie1, zombie2} local function getAge(n)         for idx=1, #Creatures do             if Creatures[idx].name == n then                     age = Creatures[idx].age                     break             end         end         return age end          print(getAge("zombie1"))  

Because you check if there is zombie with name ‘zombie1’ but there is only ‘Bob’ and ‘Sam’

Ah thank you very much appreciate it!

One little thing I would mention in relation to this (you’re actual code may already take this into account though).

Your getAge function creates and returns a variable called “age”. However you haven’t made it local so this variable “age” now exists throughout your entire program. This could cause problems if you have another variable called age somewhere else in your code. I would suggest changing the function slightly, like so:

local function getAge(n) --create a localised variable with no value local age for idx=1, #Creatures do if Creatures[idx].name == n then --overwrite age here age = Creatures[idx].age break end end --After returning the age, the function ends --and the "age" variable no longer exists in memory. return age end

If the name did not exist in the Creatures table the value of “age” will be nil, so you will need to check for that:

local myAge = getAge("Bob") if myAge then print(myAge) end

Because you check if there is zombie with name ‘zombie1’ but there is only ‘Bob’ and ‘Sam’

Ah thank you very much appreciate it!

One little thing I would mention in relation to this (you’re actual code may already take this into account though).

Your getAge function creates and returns a variable called “age”. However you haven’t made it local so this variable “age” now exists throughout your entire program. This could cause problems if you have another variable called age somewhere else in your code. I would suggest changing the function slightly, like so:

local function getAge(n) --create a localised variable with no value local age for idx=1, #Creatures do if Creatures[idx].name == n then --overwrite age here age = Creatures[idx].age break end end --After returning the age, the function ends --and the "age" variable no longer exists in memory. return age end

If the name did not exist in the Creatures table the value of “age” will be nil, so you will need to check for that:

local myAge = getAge("Bob") if myAge then print(myAge) end