Really a simple function. Using gmatch will get you where you need to go.
function sumdigits(str)
local total = 0
for digit in string.gmatch(str, "%d") do
total = total + digit
end
return total
end
print(sumdigits(1234))
10
Basically, you're looping through the integers and pulling them out one by one to add them to the total. The "%d" means just one digit, so string.gmatch(str, "%d") says, "Match one digit each time". The "for" is the looping mechanism, so for every digit in the string, it will add to the total.