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

How to evenly and randomly assign players to teams after a countdown?

Asked by
50MinZ 3
3 years ago
Edited by Rare_tendo 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

Hi, so I made a simple intermission countdown as a textbox, but I want for it to randomly assign teams after teh countdown and then kill everyone so they spawn at the spawnpoint, here is my code for the countdown which changes teh status of something which ten updates the text to say the status of it:

local status = game.ReplicatedStorage:WaitForChild("Status")
wait (5)
status.Value = "Intermission - 60"
Wait(1) 
status.Value = "Intermission - 59"
Wait(1) 
status.Value = "Intermission - 58"
Wait(1) 
status.Value = "Intermission - 57"
Wait(1) 
status.Value = "Intermission - 56"
Wait(1) 
status.Value = "Intermission - 55"
Wait(1) 
status.Value = "Intermission - 54"
Wait(1) 
status.Value = "Intermission - 53"
Wait(1) 
status.Value = "Intermission - 52"
Wait(1) 
status.Value = "Intermission - 51"
Wait(1) 
status.Value = "Intermission - 50"
Wait(1) 
status.Value = "Intermission - 49"
Wait(1) 
status.Value = "Intermission - 48"
Wait(1) 
status.Value = "Intermission - 47"
Wait(1) 
status.Value = "Intermission - 46"
Wait(1) 
status.Value = "Intermission - 45"
Wait(1) 
status.Value = "Intermission - 44"
Wait(1) 
status.Value = "Intermission - 43"
Wait(1) 
status.Value = "Intermission - 42"
Wait(1) 
status.Value = "Intermission - 41"
Wait(1) 
status.Value = "Intermission - 40"
Wait(1) 
status.Value = "Intermission - 39"
Wait(1) 
status.Value = "Intermission - 38"
Wait(1) 
status.Value = "Intermission - 37"
Wait(1) 
status.Value = "Intermission - 36"
Wait(1) 
status.Value = "Intermission - 35"
Wait(1) 
status.Value = "Intermission - 34"
Wait(1) 
status.Value = "Intermission - 33"
Wait(1) 
status.Value = "Intermission - 32"
Wait(1) 
status.Value = "Intermission - 31"
Wait(1) 
status.Value = "Intermission - 30"
Wait(1) 
status.Value = "Intermission - 29"
Wait(1) 
status.Value = "Intermission - 28"
Wait(1) 
status.Value = "Intermission - 27"
Wait(1) 
status.Value = "Intermission - 26"
Wait(1) 
status.Value = "Intermission - 25"
Wait(1) 
status.Value = "Intermission - 24"
Wait(1) 
status.Value = "Intermission - 23"
Wait(1) 
status.Value = "Intermission - 22"
Wait(1) 
status.Value = "Intermission - 21"
Wait(1) 
status.Value = "Intermission - 20"
Wait(1) 
status.Value = "Intermission - 19"
Wait(1) 
status.Value = "Intermission - 18"
Wait(1) 
status.Value = "Intermission - 17"
Wait(1) 
status.Value = "Intermission - 16"
Wait(1) 
status.Value = "Intermission - 15"
Wait(1) 
status.Value = "Intermission - 14"
Wait(1) 
status.Value = "Intermission - 13"
Wait(1) 
status.Value = "Intermission - 12"
Wait(1) 
status.Value = "Intermission - 11"
Wait(1) 
status.Value = "Intermission - 10"
Wait(1) 
status.Value = "Intermission - 8"
Wait(1) 
status.Value = "Intermission - 7"
Wait(1) 
status.Value = "Intermission - 6"
Wait(1) 
status.Value = "Intermission - 5"
Wait(1) 
status.Value = "Intermission - 4"
Wait(1) 
status.Value = "Intermission - 3"
Wait(1) 
status.Value = "Intermission - 2"
Wait(1) 
status.Value = "Intermission - 1"
Wait(2) 
status.Value = "Round starting, get ready!"
wait(1)

I want it after this to randomly and evenly assign teams and then start the game so they are moved off of the lobby team.

3 answers

Log in to vote
2
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
3 years ago

May I suggest using for loops instead of the monstrosity of code you have, no offense?

for i = 60, 1, -1 do
    status.Value = "Intermission - " .. i
    wait(1)
end

wait(2)
status.Value = "Round starting, get ready!"
wait(1)

With that dealt with, you can randomly pick a player from the game by first getting all of the players using the GetPlayers function of the Players service and then alternate between putting them in the first team and then the second team (or more teams if you have more). The simplest case I'll be handling is evenly assigning players to two teams:

local list = game.Players:GetPlayers()
local Teams = {game.Teams.Team1, game.Teams.Team2}

for _ = 1, #list do
    local index = math.random(1, #list) -- randomly choose an index that leads to a player
    local player = list[index] -- gets the randomly chosen player
    player.Team = Teams[ (#list % 2) + 1 ] -- chooses player's team; explanation below
    table.remove(list, index) -- removes the player from the list so that they aren't chosen again
end

#list % 2 finds the remainder when you divide #list by 2. In the case of the numbers being whole numbers, the result is either 0 or 1. 1 is added so that it properly gets the index of the team in the Teams table, so 0 becomes 1, and 1 becomes 2.

0
Alr, Ill try that thanks 50MinZ 3 — 3y
0
For the shorter countdown code the countdown no longer works? 50MinZ 3 — 3y
0
Did you copy and paste it as is? You're also supposed to define status before it. radiant_Light203 1166 — 3y
Ad
Log in to vote
0
Answered by
kjljixx 42
3 years ago
Edited 3 years ago
players = game:GetService("Players"):GetPlayers()
for i=1, #players do
    local RandomNumber = math.random(1,however many teams you have)
    --start using if statements to assign different values of RandomTeam to different teams e.g. 
--if RandomNumber==1 then
--AssignedTeam=game.teams.some_random_team
    players[i].team =  AssignedTeam
    --insert code to kill player
0
shall I jsut copy and paste it into the same script? 50MinZ 3 — 3y
0
Can somebody add me on discord? 50MinZ#5531 50MinZ 3 — 3y
0
well u do need to add some stuff but it should work kjljixx 42 — 3y
Log in to vote
0
Answered by
k3du53 162
3 years ago

The code you have is much longer than it needs to be. I recommend learning how to use loops in this context. https://developer.roblox.com/en-us/articles/Loops

Peep a video tutorial if you learn better that way - For Loops - Roblox Beginner Scripting #20 (AlvinBlox)

Now to randomly team the players, what I'd do is something along the lines of:

  1. assign a table of all the players in game to a variable
  2. pick a random player from the team and team them to the team with less players (and a random team if the teams have the same number)
  3. after teaming the player, remove them from the table 2 & 3 repeat using another loop until all the players are teamed

Good luck, and don't be afraid to ask more questions (with some formatting please).

Answer this question