Problem with Team Changer

Whenever a player changes a team, it also changes to others.

Server script:

local s = game.ReplicatedStorage.SwitchTeamO

local groupid = 12648144

game.Players.PlayerAdded:Connect(function(plr)
	if plr:IsInGroup(groupid) then
		s.OnServerEvent:Connect(function()
			plr.TeamColor = BrickColor.new("Navy blue")
			plr:LoadCharacter()
		end)
	end
end)


Client script:

local s = game.ReplicatedStorage.SwitchTeamO

script.Parent.MouseButton1Click:Connect(function()
	s:FireServer()
end)

Do I need to use get players? Because I’m kinda confused about GetPlayers().

1 Like

hmmmm, for team changing of group i use local script, not server one. Try use local one

I used local script already, the local script is place inside the gui button.

But wouldn’t the client only see it and not for others?

Try this:


local s = game.ReplicatedStorage.SwitchTeamO

local groupid = 12648144

s.OnServerEvent:Connect(function(plr)
    if plr:IsInGroup(groupid) then
		plr.TeamColor = BrickColor.new("Navy blue")
	    plr:LoadCharacter()
	end
end)
2 Likes

You shouldn’t use TeamColor, you can just use plr.Team. Fixed script:

local s = game.ReplicatedStorage.SwitchTeamO

local groupid = 12648144
local teamService = game:GetService("TeamService")

game.Players.PlayerAdded:Connect(function(plr)
	if plr:IsInGroup(groupid) then
		s.OnServerEvent:Connect(function()
		    plr.Team = teamService["TeamNameHere"]
			plr:LoadCharacter()
		end)
	end
end)```

GetPlayers() just returns a list (table) of players from any given team the function is called on.

local s = game.ReplicatedStorage.SwitchTeamO
local groupId = 12648144
local teamService = game:GetService("Teams")
local groupTeam = teamService[""] --change team name inside the quotes

game.Players.PlayerAdded:Connect(function(plr)
	if plr:IsInGroup(groupId) then
		s.OnServerEvent:Connect(function()
			plr.Team = groupTeam
			plr:LoadCharacter()
		end)
	end
end)

It teams me to “neutral” in-game, but in the studio, it does team me.