I'm not sure about this... but...
Changing this line
doItemSetAttribute(tmpItem, n.attr:lower(), new)
Into this one
doItemSetAttribute(tmpItem, string.lower(n.attr), new)
I didn't even check what is "n.attr" but idk I think it's getting null value and I think it's just trying to convert a...