Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make it so only 1 person gets put onto Red team and everyone else gets put onto Blue?

Asked by 10 years ago

I need it so that one person I randomly chosen to be on red and then everyone else is put on blue. So there is a chance that you could be it and the person with the highest chance gets put on red. And people with low chance get put on blue.

1 answer

Log in to vote
0
Answered by 10 years ago

To do this, you would need to get the random player, from a directory that you create. In order to achieve that, use the following code :

playerDir = game.Players:GetPlayers(); -- creates directory.
chosen = playerDir[math.random(1, #playerDir)]; -- indexes the directory and chooses a player.

From there, we need to place the chosen player into a table for referencing later on.

red = {}; -- instantiates table.
table.insert(red, chosen.Name); -- calls insert function to add the player's name to table.

Now, teaming players. We'll use a forloop to index the players and iterate through them to team them respectively.

for index,value in next, game.Players:GetPlayers() do
if not (value.Name == red[1]) then
value.TeamColor = game.Teams["Blue team"].TeamColor;
else value.TeamColor = game.Teams["Red team"].TeamColor;
end
end

The above code checks if the player is in the table and if not, teams them blue, else teams them red.

We may want to place it into a function if we need to repeatedly call it, maybe at the end/beginning of rounds.

function doTeam()
local playerDir = game.Players:GetPlayers(); -- local var to prevent out-of-scope reference
local chosen = playerDir[math.random(1, #playerDir)]; 
red = {}; 
table.insert(red, chosen.Name); 
for index,value in next, game.Players:GetPlayers() do
if not (value.Name == red[1]) then
value.TeamColor = game.Teams["Blue team"].TeamColor;
else value.TeamColor = game.Teams["Red team"].TeamColor;
end
end
end

Now to call the function, just use :

doTeam()

The whole script is :

function doTeam()
local playerDir = game.Players:GetPlayers(); -- local var to prevent out-of-scope reference
local chosen = playerDir[math.random(1, #playerDir)]; 
red = {}; 
table.insert(red, chosen.Name); 
for index,value in next, game.Players:GetPlayers() do
if not (value.Name == red[1]) then
value.TeamColor = game.Teams["Blue team"].TeamColor;
else value.TeamColor = game.Teams["Red team"].TeamColor;
end
end
end

doTeam()

Hope this helps.

  • KOTwarrior
Ad

Answer this question