Random, no repeating

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

How do I make it not repeat the same things?

One quick, simple approach:

  • Create a ‘usedNames’ table.
  • Remove each name from the ‘names’ table as it is used.
  • Add removed names to the ‘usedNames’ table.
  • When #names <= 0, repopulate it via the ‘usedNames’ table.
1 Like

Something like this?

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

Are you just looking to use table.remove()?

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

Add a task.wait() as the first line, or use Random.

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
1 Like

simply have a local random var:

RandomIndex = math.random(1, #names)
--then do
local label = v.Union.SurfaceGui.Frame.TextLabel
label.Text = "Dr. "..names[RandomIndex]
table.remove(names, RandomIndex)

do note once its removed from the table it won’t exist so you should have the names table be redeclared (not sure what ur use case is rn)

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