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

How do I get a players team in a script?

Asked by 5 years ago
Edited 5 years ago

if Player.Team == "Players" then

this doesn't work. How do I get the players team so that I can have certain team members TP somewhere in my game

I'm basically asking how can I make it so the script knows what team a player is on

0
Try finding the Color of the team. So basically if Player.TeamColor == bla bla bla mudathir2007 157 — 5y
0
To find a players team, do one of the following methods: ~~~~~~~~~~~~~~~~~ local team = player.Team local teamColor = player.TeamColor local teamName = player.Team.Name ~~~~~~~~~~~~~~~~~ with further questions/comments/concerns, feel free to reply to my answer. Happy scripting! mc3334 649 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

There are multiple ways to find a player's team.

For checking for the team's name, you can either:

  • Compare the player's Team to the corresponding Team in the Teams service:
script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if player.Team == game:GetService("Teams"):FindFirstChild("Team name") then
            -- Do stuff here
        end
    end
end)
  • Compare the player's Team's Name to a string (because the Name property itself is a string). This works because Team is a reference property that refers to the player's Team:
script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if player.Team.Name == "Team name" then
            -- Do stuff here
        end
    end
end)

If you instead want to find the team's color, then you would compare it to a BrickColor:

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if player.TeamColor == BrickColor.new("Color name") then
            -- Do stuff here
        end
    end
end)

Variant 2 of the above code:

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if player.Team.TeamColor == BrickColor.new("Color name") then
            -- Do stuff here
        end
    end
end)
Ad

Answer this question