I am trying to make a team randomizer, inside a script. I have a lobby, and I want the game to start then have the teams be randomized again.
local lobby = game.Teams.Lobby
local red = game.Teams.Red
local blue = game.Teams.Blue
while true do
--lobby stuff
wait(30)
lobby:Remove()
--team stuff here
wait(90)
end
I already have a lobby script that makes the lobby team clone into game.Teams, and my Red and Blue team have autoassignable off while the Lobby Team has autoassignable on.
(also I have a load of stuff for the one script so I’ll just use the code as a function().
You can simply use math.random and a simple loop through players to assign the teams. However there can be some issues with this including teams not being fair. This leads me to say that before creating posts like this, please look on the devforum first as the answer to your question may be there. Useful post(s) I have found (I believe with fair teaming) can be found on this thread.
(edited to correct link)
An example:
local lobby = game.Teams.Lobby
local red = game.Teams.Red
local blue = game.Teams.Blue
for i, v in pairs(game.Players:GetChildren()) do
local randomNumber = math.random(1,2)
if randomNumber == 1 then
v.Team = red
elseif randomNumber == 2 then
v.Team = blue
end
end
Not trying to be stubborn or anything, but I’m trying to keep it short and simple, with the code being similar to what is above. This sounds really amateur, but I do not understand anything they’re saying inside of their posts, especially tables.
Man, what’s considered “short and simple.” imo this is really simple. You should try it. If you dont understand, basically, the script will get all the players inside the game and will get a random number between 1 and 2(integers, which means whole numbers. No decimals). Once it gets a random number(either 1 or 2), it’ll check which number it got. If the number it got was the number 1 it would change their team to the red team. The elseif under the If statement means that if the number isn’t 1(aka false), it’ll create another if statement to check if the number is 2. If it is 2, it’ll team them to the blue team. Really simple.
local players = game.Players
local lobby = game.Teams.Lobby
local red = game.Teams.Red
local blue = game.Teams.Blue
local teams = {red, blue}
while true do
--lobby stuff
wait(30)
lobby:Remove()
for _, player in pairs(players:GetPlayers()) do
if #red:GetPlayers() > #blue:GetPlayers() then
player.Team = blue
elseif #red:GetPlayers() < #blue:GetPlayers() then
player.Team = red
else
player.Team = teams[math.random(1, #teams)]
end
end
wait(90)
end
Tried to keep it as simple as possible, this will evenly split the players between both teams.
One issue I see with this is that 1 may be randomly generated more times than 2 which would result in the red team having more players than the blue team, the same applies in the opposite direction too.