Teams

非推奨を表示

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

作成できません
サービス

チームサービスは、ゲームの Team オブジェクトを保持します。Team オブジェクトはチームサービスに親属する必要があります。

チームは、開発者にとって有用なさまざまな機能を提供します。これらは概して箱外で動作する機能と、開発者がゲームにプログラミングできる機能に分けることができます。

組み込みチームの動作 チームの次の機能はデフォルトで存在し、開発者がカスタム動作をプログラムする必要はありません。

  • チームの一部であるとき、プレイヤーのキャラクターの上の名前 ModelTeam.TeamColor に色付きになります
  • 変更する は、 に対応するチームに切り替えを引き起こします
  • デフォルトのプレイヤーリストを使用すると、ユーザーはチームによってグループ化され、表示されます
  • 設定 Player.Neutral を真に設定すると、Player がチームから切り離されますが、Player.Team または Player.TeamColor は変更されません
  • When a Player joins a game, they will be allocated to the team with Team.AutoAssignable set to true that has the fewest players.自動割り当て可能なチームがない場合、Player.Neutral が真にゲーム定されます
  • When が false に設定されると、 と一致するプレイヤーのみがその に生成できます
  • When が真に設定されると、プレイヤーの は、キャラクターが に触れると変更されます。

オプションの拡張チーム動作 多くの開発者が、自分のコードでチームに次の機能を追加することを選択しました。

  • 武器コードでチームの検査を実装して、チームの殺害を防ぐ
  • 特定のチームだけが使用できるドアや他の機能を実装する
  • 定期的にチームを再割り当ててチームバランスを維残高する

コードサンプル

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.

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)

The following code sample will create a door in the Workspace that can only be walked through by Players on the Bright red team.

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)

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.

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 し、他のInstances (またはお互い)には親オブジェクトを持たないことを推奨します。


戻り値

Instances

ゲーム内の Teams の配列。

コードサンプル

The example below prints the number of players on each Team.

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

イベント