Hey, I’m trying to make a code generator.
But I don’t know how to generate them.
math.random(1, 9000000)
Will just make numbers how can I mix numbers too?
Hey, I’m trying to make a code generator.
But I don’t know how to generate them.
math.random(1, 9000000)
Will just make numbers how can I mix numbers too?
Hello!
Not quite sure what exactly you’re looking for, I think others will be able to help you a little bit better if you elaborate a tad more. Are you trying to generate a random string, a random number or a random combination of both(or something in between).
For generating a random english character, I recommend putting the alphabet in a dictionary, like so:
local alphabet = {
[1] = "a",
[2] = "b",
[3] = "c",
-- and so on
}
and then generating a random number between 1 and 26, and indexing the alphabet dictionary by that random number.
Example:
local count = 0
local string1 = ""
repeat
count = count + 1
string1 = string1..alphabet[math.random(1,26)]
until
count == 10
print(string1) -- as long as you completed your alphabet table, this'll be a randomly generated string with 10 characters
Hope this helps.
Got an error:
Line 23: Attempt to concatenate string with nil
Not sure how to fix this.
Do I need to add the full alphabet?
Yes, you do need the full alphabet for this to work. Here’s the table to make your life a little bit easier:
local alphabet = {
[1] = "a",
[2] = "b",
[3] = "c",
[4] = "d",
[5] = "e",
[6] = "f",
[7] = "g",
[8] = "h",
[9] = "i",
[10] = "j",
[11] = "k",
[12] = "l",
[13] = "m",
[14] = "n",
[15] = "o",
[16] = "p",
[17] = "q",
[18] = "r",
[19] = "s",
[20] = "t",
[21] = "u",
[22] = "v",
[23] = "w",
[24] = "x",
[25] = "y",
[26] = "z",
}
Is there a way to mix it with numbers aswell?
You can theoretically use this method to mix your string up with any character possible, but for numbers all you have to do is add the numbers 1-9(sorry lol) to the dictionary, and make it math.random(1,35) instead of math.random(1,26).
I think you mean 0-9.
Roblox includes a method for generating globally unique identifiers. HttpService:GenerateGUID()
You can cut out a substring if you want a shorter code.
local HttpService = game:GetService("HttpService")
local result = HttpService:GenerateGUID(false)
print(string.sub(result, 1, 8)) -- >8FDF653D
I wasn’t aware of this option, certainly leagues more efficient. I’ll make sure to implement it in the future.