Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My team picking script puts everyone on the same team??

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

1 answer

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.

1 comment

0

Still having the same issue where they are both put on the same team... It doesn't make any sense at all! the script should work??? Thanks for the tip on NumPlayers though, I had no idea that existed!

This discussion is preserved as it was. New answers, comments, and votes are closed.