Assigning the player's username to their team's name

I wanted to make a script which creates a new team whenever a player joins the game and sets the name of the team to the player’s display name.
I wrote this code but I couldn’t figure out how to get a player’s display name and make it the name of the team and also put the player in the team.

game.Players.PlayerAdded:Connect(function()
	local teams = game:GetService("Teams")
	local player = game:GetService("Players")
	local newTeam = Instance.new("Team", teams)
	newTeam.Name = "Team"
	newTeam.AutoAssignable = false
end)

Hey.

You can request the players name through the PlayerAdded function and use it like so.

game.Players.PlayerAdded:Connect(function(Player) -- We request the player's name
	local teams = game:GetService("Teams")
	local player = game:GetService("Players")
	local newTeam = Instance.new("Team", teams)
	newTeam.Name = Player.DisplayName -- And we get their display name and assign it to the team's name
	newTeam.AutoAssignable = false
end)

Hope this helps.

It does not add the player to the new team. how should i do that?

You can add this to your script to team the player.

Player.Team = newTeam
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.