Tonumber keeps returning nil even when there is a number

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to fix this issue. Telling me what I did wrong could help me a lot in the future.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is, that when I use tonumber on a string, it keeps returning nil even if it has a number.03.12.2021_20.09.03_REC
    (Code is under the 3. Question)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes. I did. But I couldn’t find anything.

Code:

local random = Random.new()
local letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}

function getRandomLetter()
	return letters[random:NextInteger(1,#letters)]
end

function getRandomString(length, includeCapitals)
	local length = length or 10
	local str = ''
	for i=1,length do
		local randomLetter = getRandomLetter()
		if includeCapitals and random:NextNumber() > .5 then
			randomLetter = string.upper(randomLetter)
		end
		str = str .. randomLetter
	end
	return str
end

local function toTable(s)
	local t = {}
	s:gsub(".", function(c) table.insert(t, c) return c end)
	return t
end

while wait(1) do
	local string2 = getRandomString(10, true)
	local string2ToChar = toTable(string2)
	string2ToChar[math.random(0, string.len(string2))] = 1
	string2 = ""

	for i, v in pairs(string2ToChar) do
		string2 ..= v
	end
	print(string2)
	print(tonumber(string2))
end

Tonumber ground rule 1.

If there’s a single string, you’re meat

Try to make it only numbers

1 Like

That works but I need letters in it.

So don’t tonumber it at all. What are you trying to achieve anyway?

I think you think that tonumber gets all the numbers in the string (Excuse me if I’m interpreting this wrong)

All that tonumber does is convert a string to a number if its entirety is a valid number.

Forgot about that. xd. Thanks for reminding

tonumber is used as a way to convert a numerical string to an number value, if you just want to keep the numbers of a string you can try:

local aString = "dsan723jda82"

local only_numbers = aString:gsub("%D", "")
local an_actual_number = tonumber(only_numbers)
3 Likes

I only see you converting a single value to a number. Needs to be all numbers

local random = Random.new()
local letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}

local function getRandomLetter()
	return letters[random:NextInteger(1,#letters)]
end

local function getRandomString(length, includeCapitals)
	local length = length or 10
	local str = ""
	for i=1, length do
		local randomLetter = getRandomLetter()
		if includeCapitals and random:NextNumber() > .5 then
			randomLetter = string.upper(randomLetter)
		end
		str ..= randomLetter
	end
	return str
end

local function toTable(s)
	local t = {}
	string.gsub(s, ".", function(c) table.insert(t, c) return c end)
	return t
end

while task.wait(1) do
	local string2 = getRandomString(10, true)
	local string2ToChar = toTable(string2)
	string2ToChar[math.random(0, string.len(string2))] = 1
	string2 = ""

	for i, v in pairs(string2ToChar) do
		string2 ..= v
	end
	if tonumber(string2) then
		print(tonumber(string2))
	else
		print(string2)		
	end
end