✓ Accepted answer
First off I would like to say it is important to include the following at the top of your script:
math.randomseed(os.time());
This makes the random numbers even more random.
Then add this:
(sorry for weird formatting, I am typing this in web browser and not in an editor)
So basically what you wanna do is store all the random colors in a table, and when you generate a color, you want to check and see if it is already in use.
local colors = {};
function IsInTable(Table,Item)
for i = 1, #Table, 1 do
if (Table[i] == Item) then
return true;
end
end
return false;
end
function GenrateRandomColor()
local color = Color3.new(math.random(),math.random(),math.random());
while true do
if (IsInTable(colors,color) then
color = Color3.new(math.random(),math.random(),math.random());
else
return color;
end
end
end
function InsertColor()
for i, v in pairs (game.Players:GetPlayers()) do
local PlayerColor = Instance.new("Color3Value")
PlayerColor.Parent = v.Character
PlayerColor.Name = "Color"
local RandomColor = GenrateRandomColor();
PlayerColor.Value = Color3.new(RandomColor.r, RandomColor.g, RandomColor.b)
TweenColorScreen()
end
end
EDIT: I left out a wait(); at line 14 because it is unlikely you will need to iterate more than once to find a unique random color.
So recap:
- Use math.randomseed(os.time()) to ensure your numbers are actually random
- In the extremely unlikely case that a player will get the same three multi digit / decimal random numbers as someone else, keep track of all the colors and check whether or not it has been used yet, if so generate a new one and repeat.
4 comments
If the color is going to be used on bricks, some extra work will be needed to disambiguate between colors, because Color3 values are collapsed to some BrickColor approximations - i.e. two Color3's which are different could lead to the same BrickColor.
You forgot to add a 'wait' yield inside your iteration @Line 14
@sunwatt Good catch, but I left it out because it is highly unlikely that you will need to iterate more than once to generate a new random number
If you want your colors to be bright: local color = Color3.fromHSV(math.random(), 1, 1)