When player changes team it changes for everyone else?

My code is supposed to change the player’s team when they press the button, simple.
I have it working on both Server and Client sided but it changes all active players teams to the players team?

code:
local script

local TweenService = game:GetService("TweenService")
local Team = game:GetService("Teams")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local tweenInfo2 = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local PlayButton = script.Parent
local TeamUI = script.Parent.Parent.TeamSelectFrame
local TeamD = TeamUI.TeamD
local TeamSECURE = TeamUI.TeamSECURE
local TeamScientist = TeamUI.TeamScientist
local MainFrame = script.Parent.Parent.MainFrame
local Player = game.Players.LocalPlayer
local Players = game:GetService("Players")
local tweenPlayButton = TweenService:Create(PlayButton, tweenInfo, {Position = UDim2.new(0.389, 0, 0.31, 0)})
local tweenTeamFrame1 = TweenService:Create(TeamUI, tweenInfo, {Position = UDim2.new(0.566, 0, 2, 0)})
local tweenMainFrame1 = TweenService:Create(MainFrame, tweenInfo, {Position = UDim2.new(0.103, 0,2, 0)})

local TestSubjectPart = game.Workspace.TestSubjectPart


local CurrentCamera = game.Workspace.CurrentCamera
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

TeamSECURE.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play()
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Security
		end
	end
end)

TeamD.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play()
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams["Test Subject"]
			game.Players.LocalPlayer.Character:MoveTo(TestSubjectPart.Position)
		end
	end
end)

TeamScientist.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play() 
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Scientist
			local ScientistPartFolder = workspace.ScientistSpawnParts
			local children = ScientistPartFolder:GetChildren()
			local SpawnLocationScientist = children[math.random(1, #children)]

			Character:MoveTo(SpawnLocationScientist.Position)

		   end
		end
	end)
PlayButton.MouseButton1Click:Connect(function()
	PlayButton.Visible = false
	game.Players.LocalPlayer.PlayerGui.MainUI.Enabled = false
end)

script:

local TweenService = game:GetService("TweenService")
local Team = game:GetService("Teams")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local tweenInfo2 = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local PlayButton = script.Parent
local TeamUI = script.Parent.Parent.TeamSelectFrame
local TeamD = TeamUI.TeamD
local TeamSECURE = TeamUI.TeamSECURE
local TeamScientist = TeamUI.TeamScientist
local MainFrame = script.Parent.Parent.MainFrame
local Player = game.Players.LocalPlayer
local Players = game:GetService("Players")
local tweenPlayButton = TweenService:Create(PlayButton, tweenInfo, {Position = UDim2.new(0.389, 0, 0.31, 0)})
local tweenTeamFrame1 = TweenService:Create(TeamUI, tweenInfo, {Position = UDim2.new(0.566, 0, 2, 0)})
local tweenMainFrame1 = TweenService:Create(MainFrame, tweenInfo, {Position = UDim2.new(0.103, 0,2, 0)})

local TestSubjectPart = game.Workspace.TestSubjectPart


TeamSECURE.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play()
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Security
		end
	end
end)

TeamD.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play()
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams["Test Subject"]
		end
	end
end)

TeamScientist.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play() 
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Scientist
		end
	end
end)

I see people talking about using RemoteEvents for this but I do not know if i have to do this for every team.
Any help is appreciated.

you made it so every player’s team changes here

1 Like

Try this:

-- ServerScript

local TweenService = game:GetService("TweenService")
local Team = game:GetService("Teams")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local tweenInfo2 = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local PlayButton = script.Parent
local TeamUI = script.Parent.Parent.TeamSelectFrame
local TeamD = TeamUI.TeamD
local TeamSECURE = TeamUI.TeamSECURE
local TeamScientist = TeamUI.TeamScientist
local MainFrame = script.Parent.Parent.MainFrame
local Player = game.Players.LocalPlayer
local Players = game:GetService("Players")
local tweenPlayButton = TweenService:Create(PlayButton, tweenInfo, {Position = UDim2.new(0.389, 0, 0.31, 0)})
local tweenTeamFrame1 = TweenService:Create(TeamUI, tweenInfo, {Position = UDim2.new(0.566, 0, 2, 0)})
local tweenMainFrame1 = TweenService:Create(MainFrame, tweenInfo, {Position = UDim2.new(0.103, 0,2, 0)})

local TestSubjectPart = game.Workspace.TestSubjectPart


TeamSECURE.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play()
	
	Player.Team = game.Teams.Security
end)

TeamD.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play()
	
	Player.Team = game.Teams["Test Subject"]
end)

TeamScientist.MouseButton1Click:Connect(function()
	tweenPlayButton:Play()
	tweenTeamFrame1:Play()
	tweenMainFrame1:Play() 
	
	Player.Team = game.Teams.Scientist
end)

I got it to work on client side but it does not work on server:

StarterGui.MainUI.Play.Script:41: attempt to index nil with 'Team' 

Also, I watched a tutorial to make the players clothing change but I realized that it was running as this

local ss = game:GetService("ServerStorage")
local ScientistShirt = ss:WaitForChild("ScientistShirt")
local ScientistPants = ss:WaitForChild("ScientistPants")

local teams = game:GetService("Teams")

local function giveClothes(char, gameShirt, gamePants)
	local currShirt = char:FindFirstChildWhichIsA("Shirt")
	if not currShirt then
		currShirt = Instance.new("Shirt", char)
	end
	currShirt.ShirtTemplate = gameShirt.ShirtTemplate
	local currPants = char:FindFirstChildWhichIsA("Pants")
	if not currPants then
		currPants = Instance.new("Pants", char)
	end
	currPants.PantsTemplate = gamePants.PantsTemplate
	end

local PlayButton = game.StarterGui.MainUI.Play
PlayButton.MouseButton1Click:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(char)
		if player.Team == game.Teams.Scientist then
			giveClothes(char, ScientistShirt, ScientistPants)
		end
	end)
end)

It previously checked when the player loaded in the game but I added this to make it check for the button being clicked and the players team. This does not work or give errors?

1 Like

Change the local player:

local Player = script.Parent.Parent -- Parent this until you are in the player

For your shirt, I recommend:

-- ServerScript

local Player = script.Parent.Parent -- Parent this until you are in the player

PlayButton.MouseButton1Click:Connect(function()
	local char = Player.Character or Player.CharacterAdded:Wait()
	if Player.Team == game.Teams.Scientist then
		giveClothes(char, ScientistShirt, ScientistPants)
	end
end)

Okay so this part works but the clothing does not?

Any errors, please post them. Otherwise I can’t help.

No errors occur although when I added a print function it is not detecting that the player is clicking the button through the script, so I will try with that unless you know something else.

You don’t use StarterGui, use script.Parent. Where is even this script situated?

The clothing script is located in ServerScriptStorage

Yeah then it probably won’t work, I recommend using:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(char)
		if Player.Team == game.Teams.Scientist then
			giveClothes(char, ScientistShirt, ScientistPants)
		end
	end)
	Player:GetAttributeChangedSignal("Team"):Connect(function()
		local char = Player.Character or Player.CharacterAdded:Wait()
		if Player.Team == game.Teams.Scientist then
			giveClothes(char, ScientistShirt, ScientistPants)
		end
	end)
end)

Nope, does not work or give results. Honestly, I do not know. It is fine if you cannot.

Sorry try this:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(char)
		if Player.Team == game.Teams.Scientist then
			giveClothes(char, ScientistShirt, ScientistPants)
		end
	end)
	Player:GetPropertyChangedSignal("Team"):Connect(function()
		local char = Player.Character or Player.CharacterAdded:Wait()
		if Player.Team == game.Teams.Scientist then
			giveClothes(char, ScientistShirt, ScientistPants)
		end
	end)
end)

1 Like

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