How would i combine the separate variables into a single variable?

I’m trying to put randomly generated numbers/letters into one variable.

however, I have absolutely no idea how to do this (I’m not a good scripter lol)

here’s the code I’m using to generate the numbers:

also I have a table with all the other characters, to clarify what RANDOM_CHARACTERS is

local d1 = math.random(1, #RANDOM_CHARACTERS)
local d2 = math.random(1, #RANDOM_CHARACTERS)
local d3 = math.random(1, #RANDOM_CHARACTERS)
local d4 = math.random(1, #RANDOM_CHARACTERS)
local d5 = math.random(1, #RANDOM_CHARACTERS)
local d6 = math.random(1, #RANDOM_CHARACTERS)
local d7 = math.random(1, #RANDOM_CHARACTERS)
local d8 = math.random(1, #RANDOM_CHARACTERS)
local d9 = math.random(1, #RANDOM_CHARACTERS)
local d10 = math.random(1, #RANDOM_CHARACTERS)
local d11 = math.random(1, #RANDOM_CHARACTERS)
local d12 = math.random(1, #RANDOM_CHARACTERS)

Your probably looking for a table
local t = {2, 4, 6, 8}

Now that table holds 4 values and you can access them by number. So like t[2] would get you the second number in the table (4 in the example).

(Unless I misunderstood and you want a string instead)

To expand on this, and sorry for my lack of understanding, are you trying to create a variety of random letters to merge into one large string, potentially forming a word?

yeah i wanted to have some sort of a string with all the digits as the variable (ex “N Y a r Z k u K 3 2 L 3” as a callable variable such as PassCode)

its completely fine.

and yeah thats exactly what I’m trying to do

If that is the case then you can do it a variety of ways.

An example might be:

PassCode = d1..d2..d3..d4..d5..d6..d7..d8..d9..d10..d11..d12

While there are many more conventional ways, this is one of them.

You can make a new table that holds all the values of these, and then a for-loop to insert them one by one:

local RandomCharacters = {}

-- Declaring a for-loop like this can make it iterate from 1 to 10
for Count = 1, 10 do
    local char_index = math.random(1, #RANDOM_CHARACTERS)
    local char = RANDOM_CHARACTERS[char_index]
    table.insert(RandomCharacters[Count], char)
end

You can utilize table.concat to combine these values, like this:

local RandomizedText = table.concat(RandomCharacters, "")

This also works fine, in what you’re doing. But if you don’t want to write all those out, you can use tables:

2 Likes

Dragon_Trance got to this before me so ignore this reply.


A cleaner solution overall might be

local passCode = {}
for char = 1, 12 do
    table.insert (passCode, Random.new():NextInteger (1, #RANDOM_CHARACTERS)
end

passCode = table.concat (passCode)
2 Likes

Thank you! i really appreciate it

Lovely, math.random() is quite predictable so I urge you to use this instead.

1 Like

Here’s a tutorial for beginners.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.