Teams 服务持有游戏的 Team 对象。 Team 对象 必须归属于 Teams 服务。
Teams 提供一系列对开发者有用的功能。这些功能大致可以分为开箱即用的功能和开发者可以编程添加到游戏中的功能。
内置团队行为 Teams 的以下功能默认存在,不需要开发者编写任何自定义行为。
- 当玩家角色 Model 属于某个团队时,角色上方的名称将被着色为 Team.TeamColor
- 当使用默认玩家列表时,用户将按团队分组并显示
- 当 Player 加入游戏时,他们将被分配到团队中,Team.AutoAssignable 设置为 true 且玩家最少的团队。如果没有可自动分配的团队,Player.Neutral 将设置为 true
- 当 SpawnLocation.Neutral 设置为 false 时,只有 Player.TeamColor 匹配 SpawnLocation.TeamColor 的玩家才能在该 SpawnLocation 上生成
- 当 SpawnLocation.AllowTeamChangeOnTouch 设置为 true 时,玩家的 Player.TeamColor 将在他们的角色触碰 SpawnLocation 时更改为 SpawnLocation.TeamColor
可选扩展团队行为 许多开发者选择在自己的代码中为团队添加以下功能。
- 在武器代码中实现团队检查,以防止团队杀戮
- 实现仅特定团队可以使用的门或其他功能
- 定期重新分配团队以保持团队平衡
代码示例
此代码示例包含了如何重平衡团队的简单示例。当 Team.AutoAssignable 被设置为 true 时,玩家将以 平衡的方式被分配到团队中。然而,随着玩家离开游戏,这可能导致 团队不平衡,因为玩家没有被重新分配。此代码跟踪每个团队中的 玩家数量,并在玩家离开时检查团队是否需要重平衡。
简单的团队重平衡
local Teams = game:GetService("Teams")
-- 创建两个团队
local redTeam = Instance.new("Team")
redTeam.TeamColor = BrickColor.new("Bright red")
redTeam.AutoAssignable = true
redTeam.Name = "红色团队"
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.TeamColor = BrickColor.new("Bright blue")
blueTeam.AutoAssignable = true
blueTeam.Name = "蓝色团队"
blueTeam.Parent = Teams
-- 开始计算每个团队中的玩家数量
local numberRed, numberBlue = 0, 0
local function playerAdded(team)
-- 增加团队的计数 1
if team == redTeam then
numberRed = numberRed + 1
elseif team == blueTeam then
numberBlue = numberBlue + 1
end
end
local function playerRemoved(team)
-- 减少团队的计数 1
if team == redTeam then
numberRed = numberRed - 1
elseif team == blueTeam then
numberBlue = numberBlue - 1
end
-- 检查团队是否不平衡
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
-- 随机选择一个玩家
local playerList = bigTeam:GetPlayers()
local player = playerList[math.random(1, #playerList)]
-- 检查玩家是否存在
if player then
-- 更改玩家的团队
player.TeamColor = smallTeam.TeamColor
-- 重新生成玩家
player:LoadCharacter()
end
end
end
-- 监听玩家的添加/移除
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)
以下代码示例将在工作区创建一个门,只有亮红色团队的玩家可以通过。
团队专用门
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)
此代码示例包含一个快速函数,可添加到武器中以防止队伍击杀。当两个玩家处于不同队伍或其中任何一个玩家为中立时,它将返回 false。
队伍击杀检查
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])
概要
方法
属性
方法
GetTeams
Instances
GetTeam 函数返回一个表,其中包含游戏的 Team 对象。
请注意,这将只返回直接归属于 Teams 服务的团队对象。出于这个原因,建议开发者只将 Team 对象归属于 Teams 服务,而不是其他 Instances(或相互之间)。
返回
Instances
游戏中的 Teams 数组。
代码示例
下面的示例打印每个团队的玩家数量。
团队获取团队
local Teams = game:GetService("Teams")
local teams = Teams:GetTeams()
for _, team in pairs(teams) do
local players = team:GetPlayers()
print("团队", team.Name, "有", #players, "名玩家")
end