Reputation: 765
I need to make a code that would change the team of a player.
Let's say I have Red Team and Blue Team.
Now I want to get a player to the Blue Team by code.
How can I do that?
Upvotes: 1
Views: 3444
Reputation: 1376
The Player object have the property TeamColor. When this color matches a Team's TeamColor then the player is in that team.
If you simply want to change the Player1
's team to the Red Team
you do:
game.Players.Player1.TeamColor = game.Teams["Blue Team"].TeamColor
Also to note is that the TeamColor
is actually a BrickColor, which means that you could hardcode the color (but that requires changes to the script if you change the BrickColor of a team, however if you hardcode the TeamColor, you can change the Team name without worries, which the code abow do not allow)
local TeamRed = Instance.new("Team")
TeamRed.Name = "Team Red"
TeamRed.TeamColor = BrickColor.new("Bright red")
TeamRed.Parent = game:GetService("Teams")
-- Some other script
game.Players.Player1.TeamColor = BrickColor.new("Bright red")
However i recommend making the Teams in the code where you later use them:
local TeamRed = Instance.new("Team")
TeamRed.Name = "Team Red"
TeamRed.TeamColor = BrickColor.new("Bright red")
TeamRed.Parent = game:GetService("Teams")
local TeamBlue = Instance.new("Team")
TeamBlue.Name = "Team Blue"
TeamBlue.TeamColor = BrickColor.new("Bright blue")
TeamBlue.Parent = game:GetService("Teams")
local some_clever_named_variable = true
game.Players.PlayerAdded:connect(function(Player)
Player.TeamColor = some_clever_named_variable and TeamRed.TeamColor or TeamBlue.TeamColor
some_clever_named_variable = not some_clever_named_variable
end)
Upvotes: 1