Lets say I had a ScreenGui with 5 TextLabels
How could I change each of those 5 TextLabels to a random number between 1-99 without any of the number being a duplicate?

I know how to get random numbers but Im not every exactly sure on how I could change every TextLabel in a very optimized way without duplicate numbers

Create an array of numbers from 1-99
loop through the textlabels
generate a random index based on amount of numbers in array
assign the number into textlabel
remove the number from array

local function pickNumbers(min, max)
local taken = {};
local newNumbers = {};
for i = 1, 5, 1 do
local newNumber = math.random(min, max)
if taken[newNumber] == true then
repeat newNumber = math.random(min, max) until not taken[newNumber];
else
taken[newNumber] = true;
table.insert(newNumbers, newNumber)
end
end
return newNumbers;
end
print(pickNumbers(1,99))
--[[
Should return like:
{
[1] = "33",
[2] = "2",
[3] = "82",
[4] = "63",
[5] = "40",
}
]]

I don’t think this is optimized since your repeat loop would go through unnecessary strains until it reaches a number that isn’t chosen. Probably best to have a preset of numbers so this doesn’t happen.

local function generateNumber(min, max, list)
local num = math.random(min, max)
if table.find(list, num) then
generateNumber(min, max)
end
end
local function randomNumbers(min, max, amount): { number }
local numbers = {}
for index = 1, amount do
local num = math.random(min, max)
local startAttempt = os.clock()
if table.find(numbers, num) then
repeat
num = math.random(min, max)
until not table.find(numbers, num) or (os.clock() - startAttempt) >= 1
end
table.insert(numbers, num)
end
return numbers
end
print(randomNumbers(1, 99, 5))

local getTextLabels = script.Parent:GetChildren() --Get the children of ScreenGui. Assuming it has 5 textlabels.
for i, label in pairs(getTextLabels) do
if not label:IsA("TextLabel") then return end --Since the local script is part of getTextLabels, it will cancel that out.
local random = math.random(1,99) --Every index gives the random number. Unlikely to duplicate.
label.Text = random
end

Put math.random inside in pairs loop so that it wouldn’t duplicate. Is this what you meant? Or do you mean you want no absolute duplicates at all when the chance is unlikely? If so, I don’t exactly know but this is the best thing I could do:

local getTextLabels = script.Parent:GetChildren() --Get the children of ScreenGui. Assuming it has 5 textlabels.
local takenNumbers = {}
for i, label in pairs(getTextLabels) do
if not label:IsA("TextLabel") then return end --Since the local script is part of getTextLabels, it will cancel that out.
local random = math.random(1,99) --Every index gives the random number. Around 1% chance for the specific number to happen again.
for i,number in pairs(takenNumbers) do
if random == number then
print("Duplicates Found!")
end
end
table.insert(takenNumbers, random)
label.Text = random
end

This code should make your textlabels all have a different name without any duplicates (i may have spelt some words wrong, i cant tell cause im typing this at school xd)

local tempList = {}
for number = 1,100 do
table.append(tempList, number)
end
-- the code above puts every number from 1-100 into a list
for i,textLabel in (yourGui):GetChildren()
local index = math.random(1,#tempList)
textLabel.Name = tostring(tempList[index])
table.remove(tempList, index)
end

Basically we put every possible random number (1-100) into a list.

We loop thru all your text labels and do the following :

Pick a number from the list and set it to the name of the textlabel

Remove that number from the list so that other textlabels cannot use it again

Repeat the same steps for the rest of your textlabels