Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Silly question but is there a way to make a system like in hunger games?

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

1 answer

✓ Accepted answer

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. :)

4 comments

0

Okey, thats super complicated for my knowledge but its fine. Thanks alot! But i got more questions. So we got the system going but how would i check if some districts died? or if even all players currently playing died? I get the code but i cant find a way to make it functional. So if you could give me some examples with it, i would understand it better :)

0

Humanoids have an event called .Died, you can hook this to each player, and upon death remove them from the table. That way the table will effectively consist of alive players and nobody who has died. When all players from a team die their district's array will remain empty as {}.

0

I dont know if you still read this but how would i call the Players name? If i do print(DesiredDistrict,Players.Name) He will give me the District,yes but not the real Player Name its combined to

0

Nevermind i got it myself :) its print(DesiredDistrict,Players[Player])

This discussion is preserved as it was. New answers, comments, and votes are closed.