Teams
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Usługa Zespoły zawiera obiekty Team gra. Objekty Team muszą być powiązane z usługą Zespoły.
Drużyny oferują zbiór funkcji, które są przydatne dla rozwójców. Można je rozdzielić na funkcje, które działają poza skrzynką i funkcje, które programują rozwójcy w swojej grze.
Zachowanie zespołu wbudowane Funkcjonalność następujących funkcji zespołów jest domyślnie programowana i nie wymaga programisty do programowania dowolnego niestandardowego zachowania.
- Gdy część zespołu, nazwa ponad postacią gracza Model zostanie pokolorowana do Team.TeamColor
- Podczas używania domyślnej listy graczy użytkownicy będą grupowani i wyświetlani przez zespół
- Ustawienie Player.Neutral na prawdę spowoduje, że Player będzie odłączony od zespołu, ale nie zmieni to Player.Team lub 1> Class.Player.TeamColor1>
- Gdy Player dołącza do gry, zostaną przydzielone do zespołu z ustawieniem Team.AutoAssignable na prawdziwy, który ma najmniej graczy. Jeśli dostępny nie ma zespołu do przypisania automatycznego, Player.Neutral zostanie ustawiony na prawdziwy.
- Gdy SpawnLocation.Neutral jest ustawiony na fałszywy, tylko gracze, cuyo Player.TeamColor pasuje do SpawnLocation.TeamColor, mogą na tym 1> Class.SpawnLocation1> pojawić się.
- Gdy SpawnLocation.AllowTeamChangeOnTouch jest ustawiony na prawdę, Player.TeamColor gracza zmieni się w SpawnLocation.TeamColor gdy ich postać dotyka 1> Class.SpawnLocation1>
Opcjonalne zachowanie zespołu rozszerzonego Wiele programistów wybrało dodać następujące funkcje do zespołów w ich własnym kodzie.
- Wprowadź czynniki kontrolne dla zespołu w kodzie broni, aby zapobiec zespołowi zabijania
- Wdrożenie drzwi lub innych funkcji, które tylko niektóre zespoły mogą używać
- Okresowo przypisuj zespołom nowe nazwy, aby utrzymać równowagę zespołu
Przykłady kodu
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])
Podsumowanie
Metody
Właściwości
Metody
GetTeams
Funkcja GetTeam zwraca tabelę zawierającą Team obiekty.
Uwaga, że będzie to zwracać tylko obiekty Team, które są bezpośrednio powiązane z usługą Teams. Z tego powodu rekomendujemy, aby dla tego powodu tylko Team obiektów przypisać do usługi Teams i nie do innych 2>Class.Instance|Instances2> (lub do siebie nawzajem).
Zwroty
Materiały Teams w gra.
Przykłady kodu
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