How would I make a table that always gets unique picks?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a table that always picks unique values and not duplicates
  2. What is the issue? Include screenshots / videos if possible!
    Trying to do a math.random to grab a value from a table multiple times sometimes gets a duplicate value
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried to look up some things that people might’ve run into this, but I’ve neve really known how to implement it into my script using both colour and names, as the name random picker is also replicating the team colour name
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local possibleColors = ServerScriptService["PossibleColors"]:GetChildren()

local Team = Instance.new("Team",Teams)
Team.AutoAssignable = false
local TeamColor = (BrickColor.new(possibleColors[math.random(1,#possibleColors)].Value))

Team.TeamColor = TeamColor
Team.Name = TeamColor.Name
-- sorry for the many edits, just made it cleaner for myself and fixed a problem

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

I like using functions to get a result from a table that hasn’t been used.

local possibleColors = {"Blue", "Red", "Yellow", "Green"}
local usedColors = {}

local function getTeamColor()
	local randomColor = possibleColors[math.random(1, #possibleColors)]
	
	for i, color in pairs(usedColors) do
		if color == randomColor then
			randomColor = getTeamColor()
		end
	end
	
	return randomColor
end

local Team = Instance.new("Team", Teams)
Team.AutoAssignable = false

local randomTeamColor = getTeamColor()
table.insert(usedColors, randomTeamColor)

local TeamColor = (BrickColor.new(randomTeamColor.Value))

Team.TeamColor = TeamColor
Team.Name = TeamColor.Name

is there no way to do it from grabbing table values in a :GetChildren() table? just asking because i have 50 total colours that can be chosen from the folder list seen below

script works, just asking if this was possible.

1 Like

You can always remove the value from the folder when it’s picked and then put it back in whenever you wan’t.

oh so i can just move it to a folder called “ChosenColors”? thanks a bunch

local ServerScriptService = game:GetService("ServerScriptService")
local Teams = game:GetService("Teams")
local possibleColors = ServerScriptService:FindFirstChild("PossibleColors"):GetChildren()

local Team = Instance.new("Team")
local TeamColor = nil

local TeamColorsSelected = {};
local function ChooseColor()
	local TeamColorNew = (BrickColor.new(possibleColors[math.random(1,#possibleColors)].Value))
	if table.find(TeamColorsSelected, TeamColorNew) then
		return ChooseColor();
	else
		table.insert(TeamColorsSelected, TeamColorNew)
		TeamColor = TeamColorNew
	end
end

ChooseColor()
repeat task.wait() until TeamColor

Team.AutoAssignable = false
Team.TeamColor = TeamColor
Team.Name = TeamColor.Name
-- sorry for the many edits, just made it cleaner for myself and fixed a problem```

Yes or you can destroy them and later create new ones when a player leaves or something.

hey thanks! this works really well, only issue that it doesn’t make new team values shown on the leaderboard, and entering the team is defaulted to “Neutral”
image

1 Like

Yes, because the name is not specified

Oh wait!


local ServerScriptService = game:GetService("ServerScriptService")
local Teams = game:GetService("Teams")
local possibleColors = ServerScriptService:FindFirstChild("PossibleColors"):GetChildren()

local Team = Instance.new("Team")
local TeamColor = nil

local TeamColorsSelected = {};
local function ChooseColor()
	local TeamColorNew = (BrickColor.new(possibleColors[math.random(1,#possibleColors)].Value))
	if table.find(TeamColorsSelected, TeamColorNew) then
		return ChooseColor();
	else
		table.insert(TeamColorsSelected, TeamColorNew)
		TeamColor = TeamColorNew
	end
end

ChooseColor()
repeat task.wait() until TeamColor

print(TeamColor, typeof(TeamColor))

Team.AutoAssignable = false
Team.TeamColor = TeamColor
Team.Name = TeamColor.Name
-- sorry for the many edits, just made it cleaner for myself and fixed a problem```

Weirdly the same exact results, teams dont get created at all. Still defaults to neutral.

1 Like

The parent of the created team is not being set.

1 Like

How many times do you want to keep repeating the random check until you get one that is not a duplicate - or maybe a variable? I can make one on the spot right now

1 Like

the script i have finds folders and clones a model to each frame, then gives each of them a specific team, what i am working with right now is 6 folders it finds to clone itself in

nevermind this works, just forgot to parent the instance.new to teams :slight_smile:

3 Likes

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