local players = game.Players:GetChildren() local teams = {"Persimmon", "Cyan"} local count = 1 for i, v in pairs(players) do v.TeamColor = BrickColor.new(teams[count]) count = count + 1 v.PlayerFolder:WaitForChild("Team").Value = tostring(v.TeamColor) if count > #teams then count = 1 end end
The whole point of this script is to get random teams for every player. The only problem I have noticed is that every round I'm on the exact same team. Can anyone please help?
Of course this isn't random. You're not using a single random function.
I think what you're trying to do is create separate teams with as equal number of players as possible. Because you're already doing this, I have an idea that will compliment with your method, without changing anything.
Just create a table with the players inside, but shuffled. This will create a table of players randomly indexed.
Below is the added code which does this, combined with your code.
local players = game.Players:GetPlayers() -- I changed this to GetPlayers instead of GetChildren local teams = {"Persimmon", "Cyan"} local count = 1 for i = #players, 2, -1 do local j = math.random (i) players[i], players[j] = players[j], players[i] end for i, v in pairs(players) do v.TeamColor = BrickColor.new(teams[count]) count = count + 1 v.PlayerFolder:WaitForChild("Team").Value = tostring(v.TeamColor) if count > #teams then count = 1 end end
I haven't tested this. If there is anything wrong, let me know.
What you want is constrained random assignment:
1) The team color of each player should be random
2) At the same time, you have certain constraints, e.g. you want all teams to have about an equal amount of players
The script below satisfies both requirements. Note that LUA random implementation seems to not be "very random", but adding a bunch of extra calls to math.random()
seems to help (see explanation below).
local teams = {"Persimmon", "Cyan"} -- initialize team counts local counts = {} for i=1, #teams do counts[i]=0 end -- determine how many players per team (at most) local maxCount = math.ceil(#players / #teams) -- re-randomize every time a game starts -- NOTE: because Lua random number implementation seems to be very weak, adding a bunch of extra calls to random() can help things a bit -- reference: http://stackoverflow.com/questions/20154991/generating-uniform-random-numbers-in-lua math.randomseed(tick()) math.random(); math.random(); math.random() -- assign pseudo-random team to each player for i, v in pairs(players) do -- random team index: keep rolling the dice until we get a "good" team local teamIndex repeat teamIndex = 1 + math.floor(math.random() * #teams) math.random() math.random() math.random() math.random() until counts[teamIndex] < maxCount -- update team player count counts[teamIndex] = counts[teamIndex]+1 -- get color from index local teamColor = teams[teamIndex] print(teamColor) -- use teamColor here end
Locked by OldPalHappy
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?