Team

Hiển Thị Bản Đã Lỗi Thời

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.

Lớp Team đại diện cho một phe trong một địa điểmRoblox.Parent duy nhất hợp lệ cho một đội là trong dịch vụ Teams.Các đội cung cấp một loạt các tính năng hữu ích cho các nhà phát triển có thể được chia thành hai nhóm thô sơ:

  • Các tính năng hoạt động 'bên ngoài hộp'
  • Các nhà phát triển tính năng có thể lập trình vào trò chơi của họ.

Hành vi đội tích hợp Các chức năng sau của Nhóm tồn tại mặc định và không yêu cầu nhà phát triển phải lập trình bất kỳ hành vi tùy chỉnh nào.

Hành vi đội mở rộng tùy chọn Nhiều nhà phát triển chọn thêm các tính năng sau vào các đội trong mã của riêng họ.

  • Thực hiện kiểm tra trong mã vũ khí để ngăn chặn bắn đạn thân thiện.
  • Thực hiện kiểm tra trong các cửa hoặc các tính năng khác cho phép chỉ một số đội sử dụng chúng
  • Thường xuyên chuyển đổi đội để duy trì sự số dưbằng đội

Mẫu mã

This code sample includes a simple example of how to re-balance teams. When Team.AutoAssignable is set to true players will be added to Teams in a balanced fashion. However as Players leave the game this can lead to unbalanced teams as players are not reallocated. This code keeps track of the number of players in each team and, when players leave will check to see if the teams need re-balancing.

Simple Team Rebalance

local Teams = game:GetService("Teams")
-- create two teams
local redTeam = Instance.new("Team")
redTeam.TeamColor = BrickColor.new("Bright red")
redTeam.AutoAssignable = true
redTeam.Name = "Red Team"
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.TeamColor = BrickColor.new("Bright blue")
blueTeam.AutoAssignable = true
blueTeam.Name = "Blue Team"
blueTeam.Parent = Teams
-- start counting the number of players on each team
local numberRed, numberBlue = 0, 0
local function playerAdded(team)
-- increase the team's count by 1
if team == redTeam then
numberRed = numberRed + 1
elseif team == blueTeam then
numberBlue = numberBlue + 1
end
end
local function playerRemoved(team)
-- decrease the team's count by 1
if team == redTeam then
numberRed = numberRed - 1
elseif team == blueTeam then
numberBlue = numberBlue - 1
end
-- check if the teams are unbalanced
local bigTeam, smallTeam = nil, nil
if (numberRed - numberBlue) > 2 then
bigTeam = redTeam
smallTeam = blueTeam
elseif (numberBlue - numberRed) > 2 then
bigTeam = blueTeam
smallTeam = redTeam
end
if bigTeam then
-- pick a random player
local playerList = bigTeam:GetPlayers()
local player = playerList[math.random(1, #playerList)]
-- check the player exists
if player then
-- change the player's team
player.TeamColor = smallTeam.TeamColor
-- respawn the player
player:LoadCharacter()
end
end
end
-- listen for players being added / removed
blueTeam.PlayerAdded:Connect(function(_player)
playerAdded(blueTeam)
end)
blueTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(blueTeam)
end)
redTeam.PlayerAdded:Connect(function(_player)
playerAdded(redTeam)
end)
redTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(redTeam)
end)

This code sample includes a quick function that can be added to weapons in a place to prevent them from team killing. It will return false when the two players are on different teams or if either of them is neutral.

Team Kill Check

local Players = game:GetService("Players")
function checkTeamKill(playerAttack, playerVictim)
if playerAttack.Team ~= playerVictim.Team or playerAttack.Neutral or playerVictim.Neutral then
return false
end
return true
end
local players = Players:GetPlayers()
checkTeamKill(players[1], players[2])

The following code sample will create a door in the Workspace that can only be walked through by Players on the Bright red team.

Team Only Door

local Players = game:GetService("Players")
local door = Instance.new("Part")
door.Anchored = true
door.Size = Vector3.new(7, 10, 1)
door.Position = Vector3.new(0, 5, 0)
door.Parent = workspace
local debounce = false
door.Touched:Connect(function(hit)
if not debounce then
debounce = true
if hit then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player and player.TeamColor == BrickColor.new("Bright red") then
door.Transparency = 0.5
door.CanCollide = false
task.wait(3)
door.Transparency = 0
door.CanCollide = true
end
end
task.wait(0.5)
debounce = false
end
end)

Tóm Tắt

Thuộc Tính

  • Đọc Song Song

    Tính chất này xác định xem Players có được đặt tự động vào Team khi tham gia hay không.Nếu nhiều đội có chỉ số này được đặt thành true, Roblox sẽ cố gắng cân bằng các đội khi Players được thêm vào.

  • Đọc Song Song

    Tính chất này đặt màu của Team .Xác định thuộc tính Player.TeamColor của người chơi là thành viên của đội.Cũng xác định màu hiển thị trên danh sách người chơi và trên đầu của người chơi.

Phương Pháp

Sự Kiện

Thuộc Tính

AutoAssignable

Đọc Song Song

Tính chất này xác định xem Players có được đặt tự động vào Team khi tham gia hay không.Nếu nhiều đội có chỉ số này được đặt thành true, Roblox sẽ cố gắng cân bằng các đội khi Players được thêm vào.

Khi một Player tham gia vào một trò chơi, họ sẽ được gán cho Team với Team.AutoAssignable đặt thành true có ít người chơi nhất.Nếu không có đội như vậy, Player.Neutral sẽ được đặt thành true.

Lưu ý khi sử dụng tính năng này sẽ giúp cân bằng đội khi người chơi được thêm vào, nó sẽ không làm bất cứ điều gì khi người chơi bị xóa.Vì lý do này, các nhà phát triển có thể muốn triển khai hệ thống cân bằng đội riêng của họ.

Mẫu mã

This code sample includes a simple example of how to re-balance teams. When Team.AutoAssignable is set to true players will be added to Teams in a balanced fashion. However as Players leave the game this can lead to unbalanced teams as players are not reallocated. This code keeps track of the number of players in each team and, when players leave will check to see if the teams need re-balancing.

Simple Team Rebalance

local Teams = game:GetService("Teams")
-- create two teams
local redTeam = Instance.new("Team")
redTeam.TeamColor = BrickColor.new("Bright red")
redTeam.AutoAssignable = true
redTeam.Name = "Red Team"
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.TeamColor = BrickColor.new("Bright blue")
blueTeam.AutoAssignable = true
blueTeam.Name = "Blue Team"
blueTeam.Parent = Teams
-- start counting the number of players on each team
local numberRed, numberBlue = 0, 0
local function playerAdded(team)
-- increase the team's count by 1
if team == redTeam then
numberRed = numberRed + 1
elseif team == blueTeam then
numberBlue = numberBlue + 1
end
end
local function playerRemoved(team)
-- decrease the team's count by 1
if team == redTeam then
numberRed = numberRed - 1
elseif team == blueTeam then
numberBlue = numberBlue - 1
end
-- check if the teams are unbalanced
local bigTeam, smallTeam = nil, nil
if (numberRed - numberBlue) > 2 then
bigTeam = redTeam
smallTeam = blueTeam
elseif (numberBlue - numberRed) > 2 then
bigTeam = blueTeam
smallTeam = redTeam
end
if bigTeam then
-- pick a random player
local playerList = bigTeam:GetPlayers()
local player = playerList[math.random(1, #playerList)]
-- check the player exists
if player then
-- change the player's team
player.TeamColor = smallTeam.TeamColor
-- respawn the player
player:LoadCharacter()
end
end
end
-- listen for players being added / removed
blueTeam.PlayerAdded:Connect(function(_player)
playerAdded(blueTeam)
end)
blueTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(blueTeam)
end)
redTeam.PlayerAdded:Connect(function(_player)
playerAdded(redTeam)
end)
redTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(redTeam)
end)

TeamColor

Đọc Song Song

Thuộc tính này đặt màu của Team . Xác định thuộc tính Player.TeamColor của người chơi là thành viên của đội.

Nhiều chức năng nhóm mặc định của Roblox dựa trên màu nhóm, thay vì tên hoặc đối tượng.Ví dụ, SpawnLocations có thể được gán cho một đội thông qua SpawnLocation.TeamColor.Vì lý do này, nên khuyến khích các nhà phát triển đảm bảo mỗi Team có một TeamColor duy nhất.

Bất kỳ người chơi nào là một phần của một đội sẽ có tên của họ được thay đổi thành tên của bộ phận của đội.Họ cũng sẽ được đặt dưới tiêu đề của đội trong danh sách người chơi.

Mẫu mã

The following code sample will create a door in the Workspace that can only be walked through by Players on the Bright red team.

Team Only Door

local Players = game:GetService("Players")
local door = Instance.new("Part")
door.Anchored = true
door.Size = Vector3.new(7, 10, 1)
door.Position = Vector3.new(0, 5, 0)
door.Parent = workspace
local debounce = false
door.Touched:Connect(function(hit)
if not debounce then
debounce = true
if hit then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player and player.TeamColor == BrickColor.new("Bright red") then
door.Transparency = 0.5
door.CanCollide = false
task.wait(3)
door.Transparency = 0
door.CanCollide = true
end
end
task.wait(0.5)
debounce = false
end
end)

Phương Pháp

GetPlayers

Instances
Ghi Song Song

Trả về một danh sách của Players những người được gán cho Team .Một Player được coi là được gán nếu thuộc tính Player.Team của chúng bằng với TeamPlayer.Neutral là sai.

Chức năng này có một số cách sử dụng tiềm năng, bao gồm đếm số người chơi trên một Team hoặc cho mỗi Player trên một Team một Tool .


Lợi Nhuận

Instances

Một mảng của Players trong Team.

Mẫu mã

The example below prints the number of players on each Team.

Teams GetTeams

local Teams = game:GetService("Teams")
local teams = Teams:GetTeams()
for _, team in pairs(teams) do
local players = team:GetPlayers()
print("Team", team.Name, "has", #players, "players")
end

Sự Kiện

PlayerAdded

Bắt lửa mỗi khi có Player được gán cho Team .Một người chơi được coi là được gán nếu thuộc tính Player.Team của họ bằng với TeamPlayer.Neutral là sai.

Sự kiện này là cụ thể cho từng đội và chỉ bắn khi một Player tham gia cụ thể Team.Bất kỳ chức năng nào kết nối với sự kiện này sẽ được truyền đối tượng Player của người chơi đã tham gia vào đội.Ví dụ:

Team.PlayerAdded:Connect(function(người chơi) print(người chơi.Name.." đã tham gia vào đội) kết thúc)

Tham Số

player: Player

Các Player đã được thêm vào.


Mẫu mã

This code sample includes a simple example of how to re-balance teams. When Team.AutoAssignable is set to true players will be added to Teams in a balanced fashion. However as Players leave the game this can lead to unbalanced teams as players are not reallocated. This code keeps track of the number of players in each team and, when players leave will check to see if the teams need re-balancing.

Simple Team Rebalance

local Teams = game:GetService("Teams")
-- create two teams
local redTeam = Instance.new("Team")
redTeam.TeamColor = BrickColor.new("Bright red")
redTeam.AutoAssignable = true
redTeam.Name = "Red Team"
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.TeamColor = BrickColor.new("Bright blue")
blueTeam.AutoAssignable = true
blueTeam.Name = "Blue Team"
blueTeam.Parent = Teams
-- start counting the number of players on each team
local numberRed, numberBlue = 0, 0
local function playerAdded(team)
-- increase the team's count by 1
if team == redTeam then
numberRed = numberRed + 1
elseif team == blueTeam then
numberBlue = numberBlue + 1
end
end
local function playerRemoved(team)
-- decrease the team's count by 1
if team == redTeam then
numberRed = numberRed - 1
elseif team == blueTeam then
numberBlue = numberBlue - 1
end
-- check if the teams are unbalanced
local bigTeam, smallTeam = nil, nil
if (numberRed - numberBlue) > 2 then
bigTeam = redTeam
smallTeam = blueTeam
elseif (numberBlue - numberRed) > 2 then
bigTeam = blueTeam
smallTeam = redTeam
end
if bigTeam then
-- pick a random player
local playerList = bigTeam:GetPlayers()
local player = playerList[math.random(1, #playerList)]
-- check the player exists
if player then
-- change the player's team
player.TeamColor = smallTeam.TeamColor
-- respawn the player
player:LoadCharacter()
end
end
end
-- listen for players being added / removed
blueTeam.PlayerAdded:Connect(function(_player)
playerAdded(blueTeam)
end)
blueTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(blueTeam)
end)
redTeam.PlayerAdded:Connect(function(_player)
playerAdded(redTeam)
end)
redTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(redTeam)
end)

PlayerRemoved

Bắt lửa mỗi khi một Player được xóa khỏi một Team .Điều này có thể là do Player rời khỏi trò chơi, Player.Neutral được đặt thành true hoặc Player tham gia vào một đội khác.

Sự kiện này là cụ thể cho đội và chỉ bắt lửa khi một Player rời khỏi cụ thể Team.Bất kỳ chức năng nào kết nối với sự kiện này sẽ được truyền đối tượng Player của người chơi đã rời khỏi đội.Ví dụ:

Team.PlayerRemoved:Connect(function(người chơi) print(người chơi.Name.." đã rời khỏi đội") kết thúc)

Tham Số

player: Player

Các Player đã bị xóa.


Mẫu mã

This code sample includes a simple example of how to re-balance teams. When Team.AutoAssignable is set to true players will be added to Teams in a balanced fashion. However as Players leave the game this can lead to unbalanced teams as players are not reallocated. This code keeps track of the number of players in each team and, when players leave will check to see if the teams need re-balancing.

Simple Team Rebalance

local Teams = game:GetService("Teams")
-- create two teams
local redTeam = Instance.new("Team")
redTeam.TeamColor = BrickColor.new("Bright red")
redTeam.AutoAssignable = true
redTeam.Name = "Red Team"
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.TeamColor = BrickColor.new("Bright blue")
blueTeam.AutoAssignable = true
blueTeam.Name = "Blue Team"
blueTeam.Parent = Teams
-- start counting the number of players on each team
local numberRed, numberBlue = 0, 0
local function playerAdded(team)
-- increase the team's count by 1
if team == redTeam then
numberRed = numberRed + 1
elseif team == blueTeam then
numberBlue = numberBlue + 1
end
end
local function playerRemoved(team)
-- decrease the team's count by 1
if team == redTeam then
numberRed = numberRed - 1
elseif team == blueTeam then
numberBlue = numberBlue - 1
end
-- check if the teams are unbalanced
local bigTeam, smallTeam = nil, nil
if (numberRed - numberBlue) > 2 then
bigTeam = redTeam
smallTeam = blueTeam
elseif (numberBlue - numberRed) > 2 then
bigTeam = blueTeam
smallTeam = redTeam
end
if bigTeam then
-- pick a random player
local playerList = bigTeam:GetPlayers()
local player = playerList[math.random(1, #playerList)]
-- check the player exists
if player then
-- change the player's team
player.TeamColor = smallTeam.TeamColor
-- respawn the player
player:LoadCharacter()
end
end
end
-- listen for players being added / removed
blueTeam.PlayerAdded:Connect(function(_player)
playerAdded(blueTeam)
end)
blueTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(blueTeam)
end)
redTeam.PlayerAdded:Connect(function(_player)
playerAdded(redTeam)
end)
redTeam.PlayerRemoved:Connect(function(_player)
playerRemoved(redTeam)
end)