團隊服務持有遊戲的 Team 對象。Team 對象必須傳送到團隊服務。
團隊提供一系列有用於開發者的功能。這些可以大致分為在盒子外運行的功能和開發者可以編寫到遊戲中的功能。
內建團隊行為 以下團隊的功能預設存在,並且不需要開發人員編寫任何自訂行為。
- 當屬於團隊時,玩家角色上的名稱 Model 會被染色到 Team.TeamColor
- 當使用預設玩家列表時,使用者將被分組並由團隊顯示
- 當 SpawnLocation.Neutral 設為 false 時,只有具有 Player.TeamColor 匹配 SpawnLocation.TeamColor 的玩家才能在該 SpawnLocation 上生成
- 當 SpawnLocation.AllowTeamChangeOnTouch 設為真實時,玩家的 Player.TeamColor 將變更為 SpawnLocation.TeamColor 當他們的角色觸碰到 SpawnLocation
可選擴展團隊行為 許多開發人員選擇在自己的代碼中添加以下功能到團隊。
- 在武器代碼中實施團隊檢查以防止團隊殺手
- 實裝只有特定團隊可使用的門或其他功能
- 定期重新配置團隊以維持團隊餘額
範例程式碼
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.
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)
The following code sample will create a door in the Workspace that can only be walked through by Players on the Bright red team.
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)
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.
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
取得隊伍函數返回包含遊戲 Team 對象的表。
請注意,此操作只會返回直接屬於 Teams 服務的團隊對象。因此,建議開發人員只向Team服務傳遞父對象Teams,而不要傳遞其他Instances對象(或相互)。
返回
遊戲中的 Teams 一個陣列。
範例程式碼
The example below prints the number of players on each Team.
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