I have a very successful mini-game script part that I've worked on which is:
function InsertColor() for i, v in pairs (game.Players:GetPlayers()) do local PlayerColor = Instance.new("Color3Value") PlayerColor.Parent = v.Character PlayerColor.Name = "Color" PlayerColor.Value = Color3.new(math.random(), math.random(), math.random()) TweenColorScreen() end end
Each player would be given a random color: eg: 200, 8, 15 which would be a red dominant color
Out of the thousands of permutations possible, how do I make someone in one game not have the same color as someone else so that there could be only one winner?
Thank You.
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 -- Another player already has this color 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.