As before, I am making a game involving Team Change.
When I made a GUI with a script telling it to make a GUI visible, when I told it to change the players teams, the script crashed.
Variables(for Team Change):
1 | local Player = game.Players.LocalPlayer |
2 | local Citizen = game.Players.LocalPlayer:FindFirstChild( "Team" ):FindFirstChild( "Citizen" ) |
for the Visible part:
1 | local Confirm = game.Players.LocalPlayer:FindFirstChild( "PlayerGui" ):FindFirstChild( "Team Selector" ).Confirmation |
The function:
1 | script.Parent.MouseButton 1 Click:connect( function () |
2 | Confirm.Visible = true |
3 | Player.Team = Citizen |
4 | end ) |
Put Together:
1 | local Confirm = game.Players.LocalPlayer:FindFirstChild( "PlayerGui" ):FindFirstChild( "Team Selector" ).Confirmation |
2 | local Player = game.Players.LocalPlayer |
3 | local Citizen = game.Players.LocalPlayer:FindFirstChild( "Team" ):FindFirstChild( "Citizen" ) |
4 |
5 | script.Parent.MouseButton 1 Click:connect( function () |
6 | Confirm.Visible = true |
7 | Player.Team = Citizen |
8 | end ) |
When run, Like I said, It all crashes. What do I have to do?
There may be some confusion, but it is okay to change TeamColor or Team property when assigning a player to a team. However, by changing the Team property, you must give the actual Instance of the Team and Team must be a part of the Team Service before either changes!
Looking at your code, local Citizen = game.Players.LocalPlayer:FindFirstChild("Team"):FindFirstChild("Citizen")
seems strange. You should find a team named Citizen by looking through the TeamService. And again, keep in mind if you attempt to change TeamColor to a non-existent team's color, the player will be put in neutral or you get an error, depending on what you're doing.
See below on a script that should work, although I am unsure if changing team through localscripts will work, you may need to use Remotes to change the team. On the note of your crashes, I see nothing that should result in one, it may be a difference script you are not showing us.
1 | local Confirm = game.Players.LocalPlayer:FindFirstChild( "PlayerGui" ):FindFirstChild( "Team Selector" ).Confirmation -- unsure why you don't use FindFirstChild for Confirmation |
2 | local Player = game.Players.LocalPlayer |
3 | local Citizen = game:GetService( "Teams" ):FindFirstChild( "Citizen" ) |
4 |
5 | script.Parent.MouseButton 1 Click:Connect( function () -- Connect should be capitalized, changes nothing but is desired |
6 | Confirm.Visible = true |
7 | Player.Team = Citizen |
8 | end ) |
When you change the Team, you want to change the player's TeamColor. Look here and here! Not the team itself.
EXAMPLE (NOT BY ME):
01 | function setTeam(player, teamName) |
02 | player.TeamColor = game.Teams [ teamName ] .TeamColor |
03 | if player.Character then --Just in case the character doesn't exist for some reason |
04 | player.Character:BreakJoints() -- Kills the players' character |
05 | end |
06 | end |
07 |
08 | --How it's used |
09 | setTeam(game.Players.anth 4598 , "Blues" ) |
10 |
11 | --Changing everyone's team to "Reds" |
12 | for _, player in pairs (game.Players:GetPlayers()) do |
13 | setTeam(player, "Reds" ) |
14 | end |