So all i know is how you get the players in a group but is there a way to get like 24 players and then give each random 2 a district? Is that possible? thats what i have for now
while true do wait(2) print("choosing Tributs") _G.playing = {} for i, v in pairs(game.Players:GetPlayers()) do if v then table.insert(_G.playing, v.Name) end end end
Right - so, here is the code I have come up with to accomplish this.
function Shuffle(Table) local Size = (#Table) for i = Size, 1, -1 do local RandomObject = Random.new():NextInteger(1, Size) Table[i], Table[RandomObject] = Table[RandomObject], Table[i] end return Table end _G.Districts = {} local Players = (Shuffle(game:GetService("Players"):GetPlayers())) for i = (1),(math.ceil(#Players/2)) do _G.Districts[#_G.Districts + 1] = {} end for Player = (1),(#Players) do local DesiredDistrict = (math.ceil(Player/2)) table.insert(_G.Districts[DesiredDistrict], Players[Player]) end
Let's break it down. To start, we write a general Lua array shuffler using Fisher-Yates shuffle. This is so that we won't simply be matching players together in the order that the :GetPlayers() table provides them. From there, we move on to the more clinical stuff. We create our initial districts and players table(s). So - what next? We run a numeric for loop with the goal of creating ceiling(#players/2) districts inside of our districts table. So - after this portion of code, the following will be true:
1 player joins? >> _G.Districts = {{}}
3 players join? >> _G.Districts = {{}, {}}
4 players join? >> _G.Districts = {{}, {}}
Now what? We run yet another numerical for loop with the goal to add every player currently in the game into a district, two per district. This works because:
ceiling(1/2) > 1
ceiling(2/2) > 1
ceiling(3/2) > 2
ceiling(4/2) > 2
That's it - that's all there is to it. Now, if:
1 player: {{RandomPlayer}}
4 players: {{RandomPlayer, RandomPlayer}, {RandomPlayer, RandomPlayer}}
Hopefully this helps you. If you have any questions don't hesitate to ask. :)