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

How to change a players team?

How can I make players change team using a script? I want it to change the players team, then respawn them. Help please!

EDIT:

People said this question was vague. Sorry about that.

In my game, called "the temple" (for now) after a player conquers the temple, it changes that person to spectators team and the game starts over. Then all the 4 players and changes to the "players" team (again, a better name will be assigned later) they are then respawned. Then they are given a sword. I can write the rest. Can someone write the code up to the point I just described?

2 answers

✓ Accepted answer

In order to change the team a player is on, you need to change the TeamColor property (of the player.)

This is how it's done:

function setTeam(player, teamName)
	player.TeamColor = game.Teams[teamName].TeamColor
	if player.Character then --Just in case the character doesn't exist for some reason
		player.Character:BreakJoints() -- Kills the players' character
	end
end

--How it's used
setTeam(game.Players.anth4598, "Blues")

--Changing everyone's team to "Reds"
for _, player in pairs(game.Players:GetPlayers()) do
	setTeam(player, "Reds")
end

Tell me how that goes.

4 comments

0

Thank you!

0

no problem. If it works, accepting my answer is greatly appreciated :)

0

I kinda have a prob too... im trying to make only my certain accounts join one of my teams called owner and i set up a function for the accounts names to be identified as owners

0

Awesome script it helped me a lot

This question is extremely vague.

First off, if you are wanting an automatic team change then you can do this:

local team = "TEAM NAME"

game.Players.PlayerAdded:connect(function (newPlayer)
	newPlayer.TeamCoor = BrickColor.new(game.Teams:FindFirstChild(team).TeamColor)
	if newPlayer.Character then
		newPlayer.Character.Humanoid.Health = 0
	end
end)

Is that what you mean't? If not, please explain more.

2 comments

2

There's no point in using FindFirstChild on Teams here. Since you are getting a property from it, it will just error if it doesn't exist simply because you would be trying to get a value of nil. You can just use game:GetService("Teams")[team].TeamColor in this case.

0

Ah. The more I know, haha. Thanks.

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