I’m trying to get a script that can turn a word to a number and I found one on stackoverflow, and I have one module script that contains the numToWord function (IntegerNumberInWords(n)
) and another module that contains the wordToNum function (digitize(n_as_text)
), but whenever I try to call the numToWord function from the wordToNum function, it errors: ReplicatedStorage.gameModules.WordToNumber:6: attempt to call a Instance value
even though it’s a function. The numToWord function also works on the console but not in the wordToNum function.
wordToNum:
--[[
Credits:
https://stackoverflow.com/users/6834680/egor-skriptunoff
https://stackoverflow.com/a/46539806/18758871
https://creativecommons.org/licenses/by-sa/3.0/
]]
local IntegerNumberInWords = require(game.ReplicatedStorage.gameModules.NumberToWord)
local function digitize(number_as_text)
number_as_text = number_as_text:lower():gsub("%W", "")
for i = 1, 50 do
print(IntegerNumberInWords(i))
if IntegerNumberInWords(i):lower():gsub("%W", "") == number_as_text then
return i
end
end
end
return digitize
numToWord:
--[[
Credits:
https://stackoverflow.com/users/6834680/egor-skriptunoff
https://stackoverflow.com/a/46539806/18758871
https://creativecommons.org/licenses/by-sa/3.0/
]]
local append, concat, floor, abs = table.insert, table.concat, math.floor, math.abs
local num = {'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'}
local tens = {'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'}
local bases = {{floor(1e18), ' quintillion'}, {floor(1e15), ' quadrillion'}, {floor(1e12), ' trillion'},
{floor(1e9), ' billion'}, {1000000, ' million'}, {1000, ' thousand'}, {100, ' hundred'}}
local insert_word_AND = false -- 101 = "one hundred and one" / "one hundred one"
local function IntegerNumberInWords(n)
-- Returns a string (spelling of integer number n)
-- n should be from -2^53 to 2^53 (-2^63 < n < 2^63 for integer argument in Lua 5.3)
local str = {}
if n < 0 then
append(str, "minus")
end
n = floor(abs(n))
if n == 0 then
return "zero"
end
if n >= 1e21 then
append(str, "infinity")
else
local AND
for _, base in ipairs(bases) do
local value = base[1]
if n >= value then
append(str, IntegerNumberInWords(n / value)..base[2])
n, AND = n % value, insert_word_AND or nil
end
end
if n > 0 then
append(str, AND and "and") -- a nice pun !
append(str, num[n] or tens[floor(n/10)-1]..(n%10 ~= 0 and '-'..num[n%10] or ''))
end
end
return concat(str, ' ')
end
return IntegerNumberInWords
Line 6 (the line that’s erroring) is print(IntegerNumberInWords(i))
, but when I remove that, line 6 errors again, but it’s IntegerNumberInWords(i):lower():gsub("%W", "")
then.
Again, when I run numToWord on the console it doesn’t error at all and outputs a string, as expected.