Teams 服務 包含遊戲的 Team 對象。Teams 對象必須是繼承到 1>Teams1> 服務的。
團隊提供一系列有助於開發人員的功能。這些功能可以廣泛分為能夠從零開始運行的功能和開發人員可以程式化到他們的遊戲中的功能。
內置團隊行為 下列功能是預設的團隊行為,並且不需要開發人員程式任何自訂行為。
- 當團隊的一部分時,玩家的角色名稱上方的 Model 將會變色至 Team.TeamColor
- 使用預設玩家列表時,用戶將會被分成團隊並顯示
- 當 Player 加入遊戲時,他們將被分配到最少玩家的團隊,其中 Team.AutoAssignable 設置為 true 設為 true。如果沒有可用的自動分配團隊, Player.Neutral 將設置為 true。
- 當 SpawnLocation.Neutral 設為 false 時,只有玩家的 Player.TeamColor 與 SpawnLocation.TeamColor 一致的玩家才能在 1> Class.SpawnLocation1> 上生成
- 當 SpawnLocation.AllowTeamChangeOnTouch 設為真時,玩家的 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> (或到其他人)。
返回
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