Well, I’m trying to completely randomize some elements of the table (the number of elements to be randomly chosen is chosen), but every time it is chosen, it chooses repeated and I don’t want that to happen
local Info = {
["Spanish"] = {
{
Title = "¡Consejos!";
Texts = {
"¡Las únicas formas más frecuentes para obtener objetos consumibles, son: Comprar en las tiendas, cocinando, obtener los objetos por misiones y en regalos!";
"Uno de los mejores trabajos para obtener una buena cantidad de dinero, es el trabajo de leñador. Cada tipo de arbol tiene un valor el cuál puede llegar a ser caro.";
"gfddfg";
"jhtg";
"hytuuy";
}
}
};
["English"] = {
{
Title ="Tips!";
Texts = {
"The only most frequent ways to get consumable items are: Buying in stores, cooking, getting items from quests and gifts!";
"One of the best jobs to get a good amount of money is the lumberjack job. Each type of tree has a value which can become expensive.";
"gfddfg";
"jhtg";
"hytuuy";
}
}
};
}
local titulo = Info[Language][math.random(1, #Info[Language])]
local num = math.random(1, 3);
local texts = {};
for i = 1, num do
texts[i] = titulo.Texts[math.random(1, #titulo.Texts)];--Items are selected here
end
One method is to basically have an array of values, then pick a random position (1,#array), then use that array value however you want while also removing it from the initial table. Repeat until no values in the initial array remain.
I know, but I tried to do it and it didn’t work:
local function aleatorio(max, location)
local bool, lista = {}, {};
local j, pos, cont
for i = 1, max do
bool[i] = false
end
for i = 1, max do
pos = math.random(1, #location);
j = 0; cont = 0;
while(cont < pos)do
if(bool[j+1])then
cont += 1
end
end
j -= 1
bool[i] = true
max-=1
lista[i] = j;
end
print(lista)
end
local texts = aleatorio(math.random(1, 3), titulo)
function RandomizeArray(array)
local new = {}
for i=1,#array do
local n = math.random(1,#array) do
table.insert(new,array[n])
table.remove(array,n)
end
end
return new
end
Example:
local array = {1,2,3,4,5,6}
print(RandomizeArray(array))
Output:

(Terrible example, but it does randomize)
I wanted to add a limit of maximum three chosen elements, but it gives me this error:
ReplicatedFirst.LocalScript:56: invalid argument #2 to 'random' (interval is empty)
function RandomizeArray(max, array)
local new = {}
for i=1,max do
local n = math.random(1,#array) do
table.insert(new,array[n])
table.remove(array,n)
end
end
return new
end
local texts = RandomizeArray(math.random(1, 3), titulo.Texts);
Well that doesn’t make any sense. What line is that in the code there?
true, sorry. Is in local n = math.random(1,#array) do
function RandomizeArray(max, array)
local new = {}
for i=1,max do
if #array > 0 then
local n = math.random(1,#array)
table.insert(new,array[n])
table.remove(array,n)
end
end
return new
end
local texts = RandomizeArray(math.random(1, 3), titulo.Texts);
You added “do” to it, which isn’t needed. But just in case, I added a check as well.