I am trying to make 1/6 or less then half of the in my game playerlist to join a specific team, I don’t really know where to start since I couldn’t find help on this anywhere else.
for i, plr in pairs(game.Players:GetChildren()) do
plr.Team = TrollTeam 1/6 --Somehow get 1/6 of players on this team
end
If I understood your question right, you want to grab one sixth of the server and switch their teams?
local players = game.Players:GetPlayers()
local playersToSwitch = math.ceil(#players / 6)
for i = 1, playersToSwitch do
local chosenIndex = math.random(1, #players)
local chosenPlayer = players[chosenIndex]
--//get random players and change their team
chosenPlayer.Team = TrollTeam
--//remove chosen player index from available indices
table.remove(players, chosenIndex)
end
This would work but it doesn’t account for the team’s existing players.
local Game = game
local Players = Game:GetService("Players")
local Teams = Game:GetService("Teams")
local ExampleTeam = Teams.Example
local RandomObject = Random.new()
local function SetTeamPopulationToPercentOfServer(Team, Percent) --Percent expressed as a fraction.
local PlayersPool = Players:GetPlayers() --Pool of players to choose from.
for _, Player in ipairs(Team:GetPlayers()) do
local Index = table.find(PlayersPool, Player)
if not Index then continue end
table.remove(PlayersPool, Index) --Remove players in the desired team from the pool of players.
end
repeat
local Index = RandomObject:NextInteger(1, #PlayersPool) --Grab a random index.
table.remove(PlayersPool, Index) --Remove entry from pool of players.
PlayersPool[Index].Team = Team --Assign random player to the desired team.
until (#Team:GetPlayers() / #Players:GetPlayers()) > Percent --End loop once the team's population (as a percentage) surpasses the given percentage.
end
SetTeamPopulationToPercentOfServer(ExampleTeam, 1 / 4) --Would set the population of the example team to 25% of the server's population.