My strings just print nil

I am trying to randomly generate a 16 digit code, convert it to numbers separated by - and make the user have to solve it. When I print my codes it just shows blank.

local numberToLetter = {
    [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"
}

local letterToNumber = {
    ["A"] = 1,
    ["B"] = 2,
    ["C"] = 3,
    ["D"] = 4,
    ["E"] = 5,
    ["F"] = 6,
    ["G"] = 7,
    ["H"] = 8,
    ["I"] = 9,
    ["J"] = 10,
    ["K"] = 11,
    ["L"] = 12,
    ["M"] = 13,
    ["N"] = 14,
    ["O"] = 15,
    ["P"] = 16,
    ["Q"] = 17,
    ["R"] = 18,
    ["S"] = 19,
    ["T"] = 20,
    ["U"] = 21,
    ["V"] = 22,
    ["W"] = 23,
    ["X"] = 24,
    ["Y"] = 25,
    ["Z"] = 26
}
-- Private Functions --

function generateCode()
    local decodedCode = ""
    local code = ""
    for i = 1, 16 do -- Makes decoded code
        local chosenLetter = numberToLetter[math.random(1, #numberToLetter)]
        if math.random() > 0.5 then
            chosenLetter = string.lower(chosenLetter)
        else
            chosenLetter = string.upper(chosenLetter)
        end
    end
    print(code)
    for c in (decodedCode):gmatch'.' do
        code = code.."-"..tostring(letterToNumber[c:upper()])
    end
    return code, decodedCode
end

Your for loop doesn’t affect any variables outside it so it effectively does nothing since it just discards its variables after running. You probably have to add chosen letter to decodedcode.

Just use string.byte and string.char:
E.g.

print(string.byte("A")) -- 65
string.char(64 + math.random(1,26)) -- generates capitalized A to Z

This can lead to something along these lines.

local code = ""
for i = 1, 16 do
   local numb = math.random(1, 52)
   numb = numb > 26 and numb + 32 - 26 or numb -- switch up the + and - of the 32 and 26
   local letter = string.char(64 + numb) -- still adds 64
   code = code .. letter 
end

If you want to revert your changes just do something like this:

local decoded = {}
local code = "ABCDEFGHIJKLMNOP" -- #code = 16
for i = 1, #code do
   local currentLetter = string.sub(code,i,i)
   local numb = (string.byte(currentLetter) - 64) -- removes 64
   numb = numb > 26 and numb - 32 + 26 or numb -- this changes A = 1; Z = 26; a = 27 etc
   table.insert(decoded, numb)
end

To include the letters a to z with A to Z we would need to use the following concepts:
string.byte("a") = 97 and string.byte("z") = 122 so we would need to add 96 to the math.random(1, 26)

numb = numb > 26 and numb - 32 + 26 or numb -- this changes A = 1; Z = 26; a = 27 etc

Now we can use math.random with this variable if statement.

math.random(1, 52) 

I fixed the script it should work now.

2 Likes