Okay so this is supposed to select a random player to be chosen for the red team and put the rest of the players on the other team.
var.players.Value is the number of players in the game.
it should select a random number out of the number of players and then should catch that number on the loop while letting the other users pass to the green team.
The problem is that it's putting everyone on the red team.
Thanks in advance for the help!
local spawnPlayers = function() local chosenPlayer = math.random(var.players.Value) for i,player in pairs(game.Players:GetChildren()) do if chosenPlayer == i then player.Character.Head:destroy() player.TeamColor = BrickColor.new('Bright red') else player.Character.Head:destroy() player.TeamColor = BrickColor.new('Medium green') end end end
Instead of var.players.Value, just use NumPlayers.
Instead of Players:GetChildren(), use GetPlayers()
Capitilize :Destroy(), and move it outside of the if statement so we don't have to have it written twice.
Also, try using ipairs, so the table is always ran in the same order.
local spawnPlayers = function() local chosenPlayer = math.random(game.Players.NumPlayers) for index, player in ipairs(game.Players:GetPlayers()) do player.Character.Head:Destroy() if index == chosenPlayer then player.TeamColor = BrickColor.new('Bright red') else player.TeamColor = BrickColor.new('Medium green') end end end spawnPlayers()
Other than that, your script should be working.