There may be a more efficient way, but of the top of my head I will just give you a convert-to-string’ method.
I’ll demonstrate it in steps, and then put the whole function at the bottom.
local function explodeNumbers(myNumber) local myString = tostring(myNumber) local singleNumbers = {} --#myString gets the length of the string for i = 1, #myString do --string.sub will return a substring. --The 2nd and 3rd parameters are the start and end characters to return. --By making them both the same, we get a single character. singleNumbers[i] = string.sub(myString, i, i) print(singleNumbers[i]) end
The above should print 3, 5, 0.
To get the actual value for each digit (e.g. 300, 50, 0) you can then do:
local realValues = {} for i = 1, #singleNumbers do --consecutive numbers will be singles-\>tens-\>hundreds-\>thousands etc --so we need to calculate the power for each number realValues[i] = singleNumbers[i] \* math.pow(10, #singleNumbers-i) print(realValues[i]) end return realValues --or whatever you want to return end
Here’s the function in one chunk:
local function explodeNumbers(myNumber) local myString = tostring(myNumber) local singleNumbers = {} for i = 1, #myString do singleNumbers[i] = string.sub(myString, i, i) end local realValues = {} for i = 1, #singleNumbers do realValues[i] = singleNumbers[i] \* math.pow(10, #singleNumbers-i) end return realValues --or whatever you want to return end
And then call it with whatever number you want
local someNumbers = explodeNumbers(350)