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

How to I make a team randomizer script? (Help)

Asked by
bx3t 2
5 years ago
1local serverplayers = game.Players:GetPlayers()
2 
3    for i,v in pairs(serverplayers) do
4        serverplayers.TeamColor = BrickColor.new("Really red", "Lime green")
5    end

3 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Put the teams in an table by saying:

1local teamsTable = game.Teams:GetTeams() --creates a variable that contains all the teams (this type of variable is called a table)
2 
3 local randomTeam = teamsTable[math.random(1,#teams)] --picks a random team

Explaining the second line of code:

#teams gives us the number of teams that we have inside game.Teams

math.random() picks a random number between 1 and the number of teams (#teams) that we have

used together, math.random(1,#teams) gives us a random number

we can use that random number to get a random team in teamsTable

edit:

we worked thru the code together on discord and came up with the following code:

01matchIsInProgress = false
02 
03game.Players.PlayerAdded:Connect(function(player)
04    if(#game.Players:GetPlayers() >= 2 and not matchIsInProgress)then
05        print("there are enough players, start the match")
06 
07        matchIsInProgress = true --set the match in progress bool to true
08 
09        --You should probably tell the players somehow that the match is about to start soon here
10        wait(30) --wait 30 seconds for intermission
11 
12         --loop thru players and give each player a random team
13        local players = game.Players:GetChildren()
14 
15        for i = 1, #players do
View all 22 lines...
0
Ive explained my whole script below bx3t 2 — 5y
0
ty royaltoe 5144 — 5y
0
idk why your answer got removed as answer unless it was op removed it as answer or another mod did i just added note to your thread. no clue if your solution works or not because i've never tried changing team with team color royaltoe 5144 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You have to use BrickColor.random() to get a random brick color.

This is what your script would look like

1local players = game.Players -- player service
2 
3players.PlayerAdded:connect(function(plr) -- runs when a new player joins
4    plr.TeamColor = BrickColor.random() --  randomly assigns a player a random team color
5end)

If you want to randomize it between two teams, then you do the following:

01local players = game.Players -- player service
02local teamColors = { -- an array of team colors in the game
03    "Lime green", -- the first team color
04    "Really red" -- the second team color
05}
06 
07players.PlayerAdded:connect(function(plr) -- runs when a new player joins
08    -- assigns the new player a team based on the teams to choose from in teamColors
09    plr.TeamColor = BrickColor.new(teamColors[math.random(1,#teamColors)])
10end)
0
Ive shown my whole script below bx3t 2 — 5y
Log in to vote
0
Answered by
bx3t 2
5 years ago

Here is my TDM game script im programming so far...:

01--Variables
02local Values = game.ReplicatedStorage.Values
03local S = Values.Status
04local radiersspawn = game.Workspace.RaidersSpawn --A spawn for the team to teleport to.
05local knightsspawn = game.Workspace.KnightsSpawn --A spawn for the other team to teleport to
06local radiersplayers = game.Teams.Raiders:GetPlayers() --Raiders Team
07local knightsplayers = game.Teams.Knights:GetPlayers() --Knights Team
08local serverplayers = game.Players:GetPlayers() --Players
09local Lobbyplayers = game.Teams.Lobby:GetPlayers() --Players in the lobby
10 
11 
12while true do --Game Loop
13    if game.Players.NumPlayers == 1 then --You need 2 players to start
14        S.Value = "You need 2 (or more) players to start this game!"
15    else
View all 36 lines...
0
Sorry this is kinda hard to explain. I hope you understand bx3t 2 — 5y
0
Go to studio and then click model, then in model click services and then click Teams. Once you have Teams on explorer, insert a team and play with the properties. You can assign a name to a team color using this. InfinityEngine 223 — 5y

Answer this question