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

Is there a way to distribute random players to a random team?

I know this is a request, but I'm confused/wondering if there is a way to scramble a certain amount of random players on a certain team.

For instance, if you were making a zombie game where at the start there would be more zombies than humans, and the server consisted of 25 players, you want there to be more players than zombies. Of course, you could use math, but you would rather get it straight to the point rather than use formulas. For instance, on the game, you would like to have 20 random players be zombies, have have 5 random players be humans. Is there a way to do this? And if so, could someone please tell and explain it to me?

2 comments

0

You would use a random number to determine what team they are on http://wiki.roblox.com/index.php?title=Random_numbers. You will also have to set a counter so that the teams have the number of players you want.

0

What would a sample script look like?

1 answer

If you know the number of players, you can just get 5 random players;

local players = game.Players:GetPlayers()
local playerList = {}
for i = 1,5 do
	local chosen = players[math.random(#players)]
	table.insert(playerList, chosen)
end

Then just make everyone else a zombie.

Although you would probably want to write a function to make sure a player isn't already in the table

function IsInTable(value, array)
	for i,v in pairs(array) do
		if v == value then
			return true
		end
	end
end

But you won't know how many players are in the server, so you'd have to do math. If you want, say, 3x the amount of zombies as players, just divide the number of players in the game by 3, then choose that amount of players.

local players = game.Players:GetPlayers()
local playerList = {}
local playerNumber = math.ceil(game.Players.NumPlayers/3) --We use math.ceil just in case the number of players isn't divisible by 3.
for i = 1, playerNumber do
	local chosen = players[math.random(#players)]
	table.insert(playerList, chosen)
end

2 comments

0

How would I get the certain amount of the random players on the team?

0

What do you mean? are you asking how to change someone's team?

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