I have a random name script where it changes the textlabel’s text on the things into a set name from a table. The problem I have is I want one name appearing only one time. My script repeats the same names on different plates.
VIDEO (apologizing in advance for the poor quality):
SCRIPT:
local names = {
"Terry Barrett",
"Martha Whitlock",
"David Ester",
"Gertrude Mason",
"Lance Thatcher",
"Olivia Logan",
"Allen Hudson",
"Anna Anderson",
"Terrence Grayson",
"Charlotte William",
"Noah Lincoln",
"Trixie Wyatt",
"Jayden Scott",
"Ashley Anton",
}
for i, v in pairs(script.Parent.templates:GetChildren()) do
if v:IsA("Model") then
local label = v.Union.SurfaceGui.Frame.TextLabel
label.Text = "Dr. "..names[math.random(1, #names)]
end
end
local rnames = {
"Terry Barrett",
"Martha Whitlock",
"David Ester",
"Gertrude Mason",
"Lance Thatcher",
"Olivia Logan",
"Allen Hudson",
"Anna Anderson",
"Terrence Grayson",
"Charlotte William",
"Noah Lincoln",
"Trixie Wyatt",
"Jayden Scott",
"Ashley Anton",
}
local usedNames = {}
local function selectname()
local newname = nil
repeat
newname = Random.new():NextInteger(1, #rnames)
until not table.find(usedNames, newname)
table.insert(usedNames, newname)
return rnames[newname]
end
for i, v in pairs(script.Parent.templates:GetChildren()) do
if v:IsA("Model") then
local label = v.Union.SurfaceGui.Frame.TextLabel
label.Text = "Dr. "..selectname()
end
end
local names = {
"Terry Barrett",
"Martha Whitlock",
"David Ester",
"Gertrude Mason",
"Lance Thatcher",
"Olivia Logan",
"Allen Hudson",
"Anna Anderson",
"Terrence Grayson",
"Charlotte William",
"Noah Lincoln",
"Trixie Wyatt",
"Jayden Scott",
"Ashley Anton",
}
for i, v in pairs(script.Parent.templates:GetChildren()) do
local ran = math.random(1, #names)
if v:IsA("TextLabel") then
local label = v.Union.SurfaceGui.Frame.TextLabel
label.Text = "Dr. "..names[ran]
table.remove(names,ran)
end
end
Another option could be to shuffle the table and then loop through the shuffled version in order
-- copies `t`, shuffles the copy, and returns the copy
local function Shuffle(t)
local s = {}
for i = 1, #t do s[i] = t[i] end
for i = #t, 2, -1 do
local j = math.random(i)
s[i], s[j] = s[j], s[i]
end
return s
end
-- usage
for i, name in ipairs(Shuffle(names)) do
print(name)
end