Changing Team Problem

Hello. I made a script where you can choose what team you want to be on. There is team Red and Blue. Well, I tested my script, and when a player dies, it’ll change they’re team. For example if I was on team red and I died, then respawned, I’d be on team blue. Which that is a problem. How can I fix that?

Client Script:

local Button1 = script.Parent.Blue
local Button2 = script.Parent.Red

local Remote = game.ReplicatedStorage.changeTeam

local Color1 = "Really blue"
local Color2 = "Really red"

local plr = game.Players.LocalPlayer
local cam = workspace.CurrentCamera

repeat wait() until plr.Character or plr.CharacterAdded:Wait()

cam.CameraType = Enum.CameraType.Scriptable

cam.CFrame = workspace.cameraPart.CFrame

Button1.MouseButton1Click:Connect(function()
	Remote:FireServer(BrickColor.new(Color2))
	game.Lighting.BlurShop.Enabled = false
	wait(.02)
	cam.CameraType = Enum.CameraType.Custom

	script.Parent.Parent.Enabled = false

	game.ReplicatedStorage.OverheadTextTeamChanger:FireServer()

	local health = plr.PlayerGui.PlayScreen:FindFirstChild("Health")

	plr.Character:WaitForChild("Humanoid").MaxHealth = health.Value
	plr.Character:WaitForChild("Humanoid").Health = health.Value

	script.Parent.Visible = false

	script.Parent.Parent.Enabled = false
	for i, v in pairs(script.Parent.Parent.First:GetChildren()) do
		if v:IsA("ImageButton") or v:IsA("TextButton") or v:IsA("TextLabel")  then
			v.Visible = true
		end
	end
end)

Button2.MouseButton1Click:Connect(function()
	Remote:FireServer(BrickColor.new(Color2))
	game.Lighting.BlurShop.Enabled = false
	wait(.02)
	cam.CameraType = Enum.CameraType.Custom
	
	script.Parent.Parent.Enabled = false
	
	game.ReplicatedStorage.OverheadTextTeamChanger:FireServer()
	
	local health = plr.PlayerGui.PlayScreen:FindFirstChild("Health")

	plr.Character:WaitForChild("Humanoid").MaxHealth = health.Value
	plr.Character:WaitForChild("Humanoid").Health = health.Value
	
	script.Parent.Visible = false

	script.Parent.Parent.Enabled = false
	for i, v in pairs(script.Parent.Parent.First:GetChildren()) do
		if v:IsA("ImageButton") or v:IsA("TextButton") or v:IsA("TextLabel")  then
			v.Visible = true
		end
	end
end)

Server Script:

game.ReplicatedStorage.changeTeam.OnServerEvent:Connect(function(player, teamColor)
	player.TeamColor = teamColor
	player:LoadCharacter()
	
	game.Lighting.BlurShop.Enabled = false
	-- player.PlayerGui.PlayScreen.Enabled = false
	
	-- player.PlayerGui.Ammo.Enabled = true
	player.PlayerGui.Health.Enabled = true
	player.PlayerGui.HotBarGUI.Enabled = true
	player.PlayerGui.PlayerInfo.Enabled = true
	player.PlayerGui.Stamina.Enabled = true
	
	player.CameraMode = Enum.CameraMode.LockFirstPerson
end)

Help soon,

papahetfan

id recommend keeping a folder in replicatedstorage, and keeping a value in there so when you want to know the team when u respawn u go look for the value in replicatedstorage

I put a script in StarterPlayerScripts, and gave me an error saying “Unable to assign property Team. Object expected, got string” on line 5.

Here is the script:

local player = game.Players.LocalPlayer

repeat wait() until player.Character or player.CharacterAdded:Wait()

player.Team = game.ReplicatedStorage.myTeam.Value

game.ReplicatedStorage.myTeam.Changed:Connect(function()
	player.Team = game.ReplicatedStorage.myTeam.Value
end)

Server Script:

game.ReplicatedStorage.changeTeam.OnServerEvent:Connect(function(player, teamColor)
	player.TeamColor = teamColor
	game.ReplicatedStorage.myTeam.Value = player.Team.Name
	player:LoadCharacter()
	
	game.Lighting.BlurShop.Enabled = false
	-- player.PlayerGui.PlayScreen.Enabled = false
	
	-- player.PlayerGui.Ammo.Enabled = true
	player.PlayerGui.Health.Enabled = true
	player.PlayerGui.HotBarGUI.Enabled = true
	player.PlayerGui.PlayerInfo.Enabled = true
	player.PlayerGui.Stamina.Enabled = true
	
	player.CameraMode = Enum.CameraMode.LockFirstPerson
end)

where is this folder and value being made?

Are you using the team instance instead of a StringValue?

Do you have the AutoAssignable property unchecked for both Red and Blue teams?