チームサービスには、ゲームの Team オブジェクトが含まれています。Team オブジェクトはチームサービスに親を付ける必要があります。
チームは、開発者にとって便利な機能を提供します。これらは、「出荷」であり、開発者がゲームにプログラミングできる機能の範囲を拡大できます。
内蔵チーム動作 以下の機能は、デフォルトでチームが存在し、開発者がカスタムな動作をプログラミングする必要はありません。
- チームの一部であるとき、プレイヤーのキャラクターの上にある Model の名前は、Team.TeamColor にカラーされます。
- Class.Player.TeamColor を変更すると、Player.Team スイッチを相応する Team.TeamColor に変更します。
- デフォルトのプレイヤーリストを使用すると、ユーザーはチームによってグループ化され、表示されます
- Class.Player.Neutral を設定すると、Player はチームとの関連を解除しますが、Player.Team または 1>Class.Player.TeamColor1> を変更しません。
- Class.Player がゲームに参加すると、Team.AutoAssignable を設定しているチームに割り当てられます。Player.Neutral が利用可能でない場合は、1>Class.Player.Neutral1> が設定されます。
- Class.SpawnLocation.Neutral が偽で設定されているとき、 Player.TeamColor が SpawnLocation.TeamColor と一致するプレイヤーのみが、その 1>Class.SpawnLocation1> に生成できます。
- Class.SpawnLocation.AllowTeamChangeOnTouch が Player.TeamColor を変更すると、プレイヤーの SpawnLocation.TeamColor は、そのキャラクターが 1>Class.SpawnLocation1> に触れると 4>Class.SpawnLocation.TeamColor4> に変更されます。
拡張されたチーム動作をオプションで追加する 多くの開発者が、自分のコードでチームに以下の機能を追加しました。
- チームを殺すことを防ぐために、武器コードのチェックを実装します
- ドアや他の機能を実装することで、特定のチームだけが使用できる機能を実装します
- チームを定期的に再割り当ててチームのバランスを維残高する
コードサンプル
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
GetTeam 関数は、ゲームの Team オブジェクトを含むテーブルを返します。
注意することは、Teams サービスに直接親切されるチームオブジェクトのみを返すことです。これは、Team サービスにのみ親切される必要があるためです。このため、開発者は Teams オブジェクトを 2>Class.Teams2> サービスにのみ
戻り値
ゲーム中の 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