Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --https://youtu.be/rvva4TSGunI
- local Players = game:GetService("Players")
- local Teams = game:GetService("Teams")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local teamPickValue = ReplicatedStorage:FindFirstChild("TeamPick")
- if not teamPickValue then
- teamPickValue = Instance.new("StringValue")
- teamPickValue.Name = "TeamPick"
- teamPickValue.Parent = ReplicatedStorage
- end
- -- Ensure red/blue teams exist
- local function ensureTeamsExist()
- local redTeam = Teams:FindFirstChild("Red")
- local blueTeam = Teams:FindFirstChild("Blue")
- if not redTeam then
- redTeam = Instance.new("Team")
- redTeam.Name = "Red"
- redTeam.TeamColor = BrickColor.new("Bright red")
- redTeam.Parent = Teams
- end
- if not blueTeam then
- blueTeam = Instance.new("Team")
- blueTeam.Name = "Blue"
- blueTeam.TeamColor = BrickColor.new("Bright blue")
- blueTeam.Parent = Teams
- end
- end
- ensureTeamsExist()
- --Assign players to the team with fewer players
- Players.PlayerAdded:Connect(function(player)
- local redPlayers = {}
- local bluePlayers = {}
- for _, p in pairs(Players:GetPlayers()) do
- if p.Team and p.Team.Name == "Red" then
- table.insert(redPlayers, p)
- elseif p.Team and p.Team.Name == "Blue" then
- table.insert(bluePlayers, p)
- end
- end
- if #redPlayers <= #bluePlayers then
- player.Team = Teams:FindFirstChild("Red")
- else
- player.Team = Teams:FindFirstChild("Blue")
- end
- end)
- --Choose a random player from a team with 2 or more members
- local function chooseFromTeamWithTwoPlayers()
- for _, team in pairs(Teams:GetChildren()) do
- local teamList = {}
- for _, player in pairs(Players:GetPlayers()) do
- if player.Team == team then
- table.insert(teamList, player)
- end
- end
- if #teamList >= 2 then
- local teamChosen = teamList[math.random(1, #teamList)]
- teamPickValue.Value = "Team Pick: " .. teamChosen.Name
- return
- end
- end
- teamPickValue.Value = "No team has at least two players"
- end
- -- Delay to allow players to load in
- task.wait(20)
- chooseFromTeamWithTwoPlayers()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement