Answered by
8 years ago Edited 8 years ago
First off I would like to say it is important to include the following at the top of your script:
1 | 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.
03 | function IsInTable(Table,Item) |
04 | for i = 1 , #Table, 1 do |
05 | if (Table [ i ] = = Item) then |
12 | function GenrateRandomColor() |
13 | local color = Color 3. new(math.random(),math.random(),math.random()); |
15 | if (IsInTable(colors,color) then |
17 | color = Color 3. new(math.random(),math.random(),math.random()); |
25 | for i, v in pairs (game.Players:GetPlayers()) do |
26 | local PlayerColor = Instance.new( "Color3Value" ) |
27 | PlayerColor.Parent = v.Character |
28 | PlayerColor.Name = "Color" |
29 | local RandomColor = GenrateRandomColor(); |
30 | PlayerColor.Value = Color 3. new(RandomColor.r, RandomColor.g, RandomColor.b) |
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.