Advertisement
Parallaxox

Team Pick SS

Apr 8th, 2025
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1. --https://youtu.be/rvva4TSGunI
  2. local Players = game:GetService("Players")
  3. local Teams = game:GetService("Teams")
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5.  
  6. local teamPickValue = ReplicatedStorage:FindFirstChild("TeamPick")
  7. if not teamPickValue then
  8.     teamPickValue = Instance.new("StringValue")
  9.     teamPickValue.Name = "TeamPick"
  10.     teamPickValue.Parent = ReplicatedStorage
  11. end
  12.  
  13. -- Ensure red/blue teams exist
  14. local function ensureTeamsExist()
  15.     local redTeam = Teams:FindFirstChild("Red")
  16.     local blueTeam = Teams:FindFirstChild("Blue")
  17.  
  18.     if not redTeam then
  19.         redTeam = Instance.new("Team")
  20.         redTeam.Name = "Red"
  21.         redTeam.TeamColor = BrickColor.new("Bright red")
  22.         redTeam.Parent = Teams
  23.     end
  24.  
  25.     if not blueTeam then
  26.         blueTeam = Instance.new("Team")
  27.         blueTeam.Name = "Blue"
  28.         blueTeam.TeamColor = BrickColor.new("Bright blue")
  29.         blueTeam.Parent = Teams
  30.     end
  31. end
  32.  
  33. ensureTeamsExist()
  34.  
  35. --Assign players to the team with fewer players
  36. Players.PlayerAdded:Connect(function(player)
  37.     local redPlayers = {}
  38.     local bluePlayers = {}
  39.  
  40.     for _, p in pairs(Players:GetPlayers()) do
  41.         if p.Team and p.Team.Name == "Red" then
  42.             table.insert(redPlayers, p)
  43.         elseif p.Team and p.Team.Name == "Blue" then
  44.             table.insert(bluePlayers, p)
  45.         end
  46.     end
  47.  
  48.     if #redPlayers <= #bluePlayers then
  49.         player.Team = Teams:FindFirstChild("Red")
  50.     else
  51.         player.Team = Teams:FindFirstChild("Blue")
  52.     end
  53. end)
  54.  
  55. --Choose a random player from a team with 2 or more members
  56. local function chooseFromTeamWithTwoPlayers()
  57.     for _, team in pairs(Teams:GetChildren()) do
  58.         local teamList = {}
  59.         for _, player in pairs(Players:GetPlayers()) do
  60.             if player.Team == team then
  61.                 table.insert(teamList, player)
  62.             end
  63.         end
  64.  
  65.         if #teamList >= 2 then
  66.             local teamChosen = teamList[math.random(1, #teamList)]
  67.             teamPickValue.Value = "Team Pick: " .. teamChosen.Name
  68.             return
  69.         end
  70.     end
  71.  
  72.     teamPickValue.Value = "No team has at least two players"
  73. end
  74.  
  75. -- Delay to allow players to load in
  76. task.wait(20)
  77. chooseFromTeamWithTwoPlayers()
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement