Your function
local function replaceingredients(ingredients)
for _,ingredient in pairs(ingredients) do
local nameidx = 1
if ingredient.name then nameidx = 'name' end
local r = replaceingredient[ingredient[nameidx]]
if r then
ingredient[nameidx] = r
end
end
end
goes error if there is no ingredients, add check so that it is
local function replaceingredients(ingredients)
if ingredients then
for _,ingredient in pairs(ingredients) do
local nameidx = 1
if ingredient.name then nameidx = 'name' end
local r = replaceingredient[ingredient[nameidx]]
if r then
ingredient[nameidx] = r
end
end
end
end