Help with setting the team of a random character

I want my script to generate a random character preset and set this character to it’s own team.

My issue is that I cannot or do not know how to set the player’s team once it generates the character.

I have looked through many posts on the forums but none of them have helped me.

while wait(0.5) do
local GenRand = math.random(1,3)
	
	if GenRand == 1 then
		game.StarterPlayer:FindFirstChild("StarterCharacter"):Destroy()
		game.ServerStorage.Char1.StarterCharacter:Clone().Parent = game.StarterPlayer

		game.player.TeamColor = BrickColor.new("White")
		
	elseif GenRand == 2 then
		game.StarterPlayer:FindFirstChild("StarterCharacter"):Destroy()
		game.ServerStorage.Char2.StarterCharacter:Clone().Parent = game.StarterPlayer
		
		game.player.TeamColor = BrickColor.new("Really black")
		
	elseif GenRand == 3 then
		game.StarterPlayer:FindFirstChild("StarterCharacter"):Destroy()
		game.ServerStorage.Neil.StarterCharacter:Clone().Parent = game.StarterPlayer
		
		game.player.TeamColor = BrickColor.new("Deep orange")
			end
	end

I think the issue here may be that you are trying to set the team of the player using TeamColor without actually creating any teams first. For more info on teams, check out this page in the Roblox API reference!

Using a server script, you can make the teams. Here is an example of a script that makes two teams.

local Teams = game:GetService("Teams")

local hostTeam = Instance.new("Team",Teams)
hostTeam.TeamColor = BrickColor.new("Gold")
hostTeam.AutoAssignable = false
hostTeam.Name = "Host"

local judgeTeam = Instance.new("Team",Teams)
judgeTeam.TeamColor = BrickColor.new("Bright red")
judgeTeam.AutoAssignable = false
judgeTeam.Name = "Judges"

One thing to keep in mind is that you should set AutoAssignable to false, that way players will not join the team by default. You will still be able to change the team of the player using a script.

Once you have a script to actually build the teams, then you can set the player’s team!

Let me know if this does not work, or you have any questions.

This did not help much with the problem. It did work in creating the teams but I may have made my intentions difficult to understand. My problem was mainly in setting the players who joined into one of the 3 teams but only let those with a specific character model into the team. As of now what is happening is the player is starting in the neutral team and not moving to other teams. The teams are already there but I don’t know how to send the player into the team after creating the character.