Search code examples
lualua-table

Lua : return table trough function is nil


I get the error: source_file.lua:5: attempt to call a nil value (global 'getCard')

i try to catch the right table in questCards which Index=name is the same as the given string from objName

questCards={{['name']='test1',['creatureName']='test3'},{['name']='test2',['creatureName']='test4'}}
obj='test1'
card=getCard(obj)
card['creatureName']=nil --Only for test purpose
if card['creatureName']==nil then
    --do Somthing
end
function getCard(objName)
  for k,v in pairs(questCards) do
    if v['name']==objName then
      return v
    end
  end
end

Solution

  • The error message is telling you that getCard is not defined at the point where it is called.

    You need to define getCard before calling it.