Sorry for kind of breaking the rules here, but I don't know how to code this and I need it for something I'm making

I am trying to make a file explorer for Roblox Studio, and I need one, simple function to get everything working. I need a function that will create a string, using all possibilities, and stopping at a maximum length of 20 characters. For example, it should go: a, b, c, d, e, f, g… …z, aa, ba… za, ab, bb…
I hope you get the idea, because without this, I can’t create my file explorer! D:

Solutions I’ve tried: coding it myself, using AI to get the math right (jesus christ), contemplating my life’s descisions, winning the lottery, and now this.

So you want a hexadecimal string generator?

I guess, but it needs to go in an order from lowercase a to 0 (the string would be “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890”). So basically, it would cover every possible filename within 20 characters length.

So do you want a random character generator that generates 20 lowercase, 20 uppercase characters and 20 numbers? Unless I misunderstood your request.

No, I can guarantee you misunderstood. I do NOT want it to be random, I want it to go like this: “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, aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an, ao, ap, aq, ar, as, at, au, av, aw, ax, ay, az, ba, bb, bc, bd, be” etc. etc. I want it to be a kind of ‘pattern’ that goes up all the way until “zzzzzzzzzzzzzzzzzzzzz”. It should use an input value, by the way, so an input of 1 would be “a”, an input of 26 would be “z”, etc.

You’re describing a pattern in which there are up to 20 characters that each may use 1 of the 26 available letters in the English alphabet. To calculate the number of possible patterns, we combine the Fundamental Counting Theorem with a Geometric Series to sum up all patterns of length 1 - 20:

image

image

This yields the following: 20,725,274,851,017,785,518,433,805,270
Aka. ~20 octillion.

This number is far beyond what Luau can naively handle. In order to develop the algorithm you require, you must use a Big Num library. Why why on God’s green Earth do you need this for your Explorer?

Edit: I just realized that you don’t just want strings in lowercase, you want uppercase letters and numbers as well. This is easy to update in the code (just requires an update to getChar plus changing 26 to 62 in the code), but the maximum string length will be significantly lower because you’re working with extremely large numbers (look at the warning at the bottom of my post). This is looking like an XY problem.

If you only want a string with the letters a-z

The resulting string is essentially a number in base-26 except all the digits are letters. You can choose specific strings using the following formula:


Notice how 26^0 = 1, so you can simplify A_0 * 26^0 to just A_0 when choosing the last digit.
I wrote this formula in PDF format using LaTeX in case you’re wondering.

You don’t have to use this formula, but if you want a specific string then that formula is how you get it. Here is how you put this into code:

local BASE = 26

-- Maps the characters a-z to 0-25.
local function getChar(index: number) : string
	return string.char(97 + index)
end

--[[ The input to this function can be of the form:
	A_{n-1}*26^(n-1) + A_{n-2}*26^(n-2) + ... + A_0*26^0
where n is the number of digits in your string,
and A_i is a number from 1-26 which chooses the letter for the i-th digit.

For example,
	- 1 maps to "a"		(1 = 1*26^0))
	- 27 maps to "aa"	(27 = 1*26 + 1*26^0)
	- 28 maps to "ab"	(28 = 1*26 + 2*26^0)
	- 1*26^2 + 1*26^1 + 1*26^0 maps to "aaa"
	- 2*26^2 + 3*26^1 + 1*26^0 maps to "bca" because 2->"b", 3->"c", 1->"a"
	- 26*26^2 + 26*26^1 + 26*26^0 maps to "zzz"

]]
local function getStringOfLetters(input: number): string
	local s = ""

	repeat
		input -= 1
		local digit = input % BASE
		s = getChar(digit) .. s
		input = math.floor(input / BASE)
	until input <= 0
	-- Technically you only need "until input == 0",
	-- but making it "until input <= 0" lets you prevent
	-- bugs in your code from making this loop run forever.

	return s
end

If you want the string “aaaa” (four digits) then the while loop will run 4 times. This means that for all practical purposes you don’t need a task.wait() in the while loop (one iteration per character in the final string).

If you want to include a-z, A-Z, and 0-9 (only works if you use a big num library because the numbers are too large for Lua)

The resulting string is number in base-62 except all the digits are letters. You can choose specific strings using the following formula:

local BASE = 62

-- Maps the numbers 0-25 to a-z, 26-51 to A-Z, and 52-62 to 0-9.
local function getChar(index: number): string
	if index < 26 then
		return string.char(97 + index) -- a-z
	elseif index < 52 then
		return string.char(65 + (index - 26)) -- A-Z
	elseif index < 62 then
		return string.char(48 + (index - 52)) -- 0-9
	else
		error("index out of range")
	end
end

--[[ You can choose each individual digit by using the following formula:
	A_n*62^n + A_n-1*62^n-1 + ... + A_0*62^0
where n is the number of digits in your string,
and A_i is a number from 1-26 which chooses the letter for the i-th digit.

For example,
	- 1 maps to "a"		(1 = 1*62^0))
	- 27 maps to "aa"	(27 = 1*62 + 1*62^0)
	- 28 maps to "ab"	(28 = 1*62 + 2*62^0)
	- 2*62^2 + 3*62^1 + 1*62^0 maps to "bca" because 2->"b", 3->"c", 1->"a"
	
Notice how 62^0 = 1 so you can simplify A_0*62^0 to just A_0 for the last digit.
]]
local function getStringOfLetters(input: number): string
	local s = ""

	repeat
		input -= 1
		local digit = input % BASE
		s = getChar(digit) .. s
		input = math.floor(input / BASE)
	until input <= 0
	-- Technically you only need "until input == 0",
	-- but making it "until input <= 0" lets you prevent
	-- bugs in your code from making this loop run forever.

	return s
end

WARNING:

The base-26 function (letters a-z) only works for strings up to 11 characters long because after that point you’re working with numbers larger than what Lua can handle. This limit is made significantly worse (practically unusable) if you want to include ALL characters a-z, A-Z, and 0-9. To fix this you would need to use some big num library as mentioned by @Ziffix, but if you really need to do this then I question why you need it to begin with.

i dont understand why this function would help you build a file explorer i think a function to alphabetically sort a list should be much simpler and do what i assume you are trying to do

Come on.
The only use I can think of for using aa, ab, ac would be if you have a shit-ton of randomly named objects/files and you sort them into folders with names that begin with the first two characters of the randomly named objects/files. But even in this case, you might not need to generate those prefixes anyway.

I’m trying to browse the user’s PC, and so I would need to have every possible filename with simple characters, then check if the asset loads, and if it does, put it on a list.

that would take basically forever for things named longer than like 3-4 characters since you would be making thousands of api calls there are 456 thousand 4 character strings

My code does what you’re asking for, but what you’re trying to do is practically infeasible even if we ignore the fact that Lua can’t represent extremely large numbers . You’re brute-forcing every possible file name.

If each character has 62 possibilities (a-z, A-Z, 0-9) and every file name is exactly 20 characters, then you need to check 62^20 possibilities. This doesn’t even account for file names less than 20 characters, yet the number of names you need to check in this case is already impossibly large.

There absolutely must be a better way to get all files.

Okay, then how could I possibly see every file the player has?

Why would you ever need to do that? First, is this in studio or in the client? There is no way for you to query the users computer and get a list of all their files whether you are in plugin context or not.

If you are in a plugin, you can ask the user to input files using StudioService: PromptImportFileAsync() which takes in an optional array of file extensions

To manipulate or read the data you can use the file methods

What if I want to customize the file eplorer? No, I am not using that async.

couldnt you just put all the letters and numbers and stuff into a table or am i getting this wrong

The table you would generate would be astronomically large

ohh now i get it, but couldnt you just make a function that goes through all possibilities, like a loop for A so it will loop a then aa then ab then ac then ad ect, then next letter b, ba, bb, bc

you could put all the letters into a table then loop through the table and do the thing i said again idk if im getting this wrong

The resulting table would be astronomically large. It’s best the pattern be generated upon request, which is what OP attempted to build an algorithm for. The algorithm is simple and relatively performant, but its necessity in OP’s File Explorer is incredibly questionable. Earlier, I estimated the number of possible patterns to be roughly 20,725,274,851,017,785,518,433,805,270, which is egregious

ok but isnt that what OP wants??? a string with all the letters and possibilities? idk how you wouldnt make it big i think i might be to dumb for this lol