团队服务持有游戏的 Team 对象。 Team 对象必须是团队服务的父级。
团队提供一系列有助于开发人员的功能。这些功能可以被广泛分为可以在箱子中运行的功能和开发人员可以在游戏中编程的功能。
内置团队行为 团队的以下功能默认存在,无需开发人员编写任何自定义行为。
- 当一个团队的一部分时,玩家角色上方的名称将被着色为 Model
- 使用默认玩家列表时,用户将被团队化并显示在
- 当 Player 加入游戏时,他们将被分配到拥有最少玩家的团队,其中设置为 Team.AutoAssignable 为真实值,该为自动分配的团队不可用。如果没有可用的自动分配团队,Player.Neutral 将设置为真实值。
- 当 SpawnLocation.Neutral 设置为 false 时,只有玩家 whose Player.TeamColor 与 SpawnLocation.TeamColor 匹配的玩家才能在该 1> Class.SpawnLocation1> 上生成
- 当 SpawnLocation.AllowTeamChangeOnTouch 设置为 true 时,玩家的 Player.TeamColor 将更改为 SpawnLocation.TeamColor ,当他们的角色触摸到 1> Class.SpawnLocation1> 时。
可选的扩展团队行为 许多开发人员选择为他们自己的代验证码添加以下功能。
- 为武器代码实现检查,防止团队被杀
- 实现只有特定团队才能使用的门或其他功能
- 定期重新分配团队以保持团队平余额
代码示例
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)
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)
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])
概要
方法
属性
方法
GetTeams
Instances
GetTeam 函数返回包含游戏 Team 对象的表。
注意这将只返回直接父级于 Teams 服务的对象。为此,建议开发人员仅将 Team 对象父级于 Teams 服务,而不是其他 1>Class.Instance|Instances1> (或于 4>Class.Instance|Instances4> 之间)。
返回
Instances
游戏中的 Teams 阵列。
代码示例
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