Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Do I Change The TeamColor Of A Player?

Hi guys please help me out, because I don't know and I've been trying random things but they don't work... place is not FE.

game.Players.PlayerAdded:Connect(function(plyr)

script.Parent.MouseButton1Down:Connect(function()

plyr.TeamColor = BrickColor.new("Pastel Blue")

end)

end)

ive done things like making the neutral thing off i just dont know anymore pls help

1 comment

0

I want the player to switch teams when they press the textbutton from neutral to the other team

1 answer

✓ Accepted answer

You're listening for game.Players.PlayerAdded, which is an event that fires when a player joins. This isn't quite what you want, but it's great that you tried to solve this problem yourself before asking.

We can use the Player.Team rather than the Player.TeamColor property to make our code clearer.

We need two scripts. One, server-side, should handle requests from the client (player's computer) to change teams. It should check if the team requested is one that can be switched to, and if so, switch the player's team. The client-side code should listen for button presses and fire the server when pressed.

Server-side Script code in game.ServerScriptService:

local TeamsService = game:GetService("Teams")

local TeamSwitch = game.ReplicatedStorage.Network.TeamSwitch

TeamSwitch.OnServerEvent:Connect(function(Player, Team)
	if typeof(Team) ~= "instance" then
		return
	end

	if not Team:IsA("Team") or Team.Parent ~= TeamsService then
		return
	end

	Player.Team = Team
end)

Client-side LocalScript code in the GUI button (I recommend moving this to game.StarterPlayer.StarterPlayerScripts, as scripts really don't belong in GUI objects):

local TeamsService = game:GetService("Teams")

local LocalPlayer = game.Players.LocalPlayer

local TeamSwitch = game.ReplicatedStorage.Network.TeamSwitch

local Button = script.Parent

Button.MouseButton1Click:Connect(function()
	local TeamSwitchTarget = nil
	-- We could use the and / or operators here if we wanted to.
	if LocalPlayer.Team == TeamsService.Red then
		TeamSwitchTarget = TeamsService.Blue
	else
		TeamSwitchTarget = TeamsService.Red
	end

	TeamSwitch:FireServer(TeamSwitchTarget)
end)

Note that Network is a folder, and TeamSwitch is a RemoteEvent.

2 comments

0

if a team called white walkers and people normally spawn on neutral team how would u make it like that

0

If you only want their team set once they press the button, modify my client-side code to do just that inside the button event listener. However, if there are other teams that you do not want players to be able to switch to, I would change my server-side RemoteEvent handling to check for this.

This discussion is preserved as it was. New answers, comments, and votes are closed.