Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How to change a players team? [closed]

Asked by 10 years ago

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?

Locked by JesseSong

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
5
Answered by
nate890 495 Moderation Voter
10 years ago

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.

0
Thank you! Antharaziia 75 — 10y
0
no problem. If it works, accepting my answer is greatly appreciated :) nate890 495 — 10y
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 illuminatidrag0ns0ul 2 — 6y
0
Awesome script it helped me a lot funnytevinn -5 — 4y
Ad
Log in to vote
0
Answered by 10 years ago

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
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. User#11893 186 — 10y
0
Ah. The more I know, haha. Thanks. AmericanStripes 610 — 10y