Team

Pokaż przestarzałe

*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.

Klasa Team reprezentuje frakcję na miejsceRoblox.Jedynym ważnym rodzicem dla zespołu jest usługa Teams .Drużyny oferują szereg funkcji, które są przydatne dla programistów, które można podzielić na dwie ogólne grupy:

  • Funkcje, które działają "poza pudełkiem"
  • Funkcje, które programiści mogą wprowadzić do swojej gry.

Zintegrowane zachowanie zespołu Poniższa funkcjonalność zespołów istnieje domyślnie i nie wymaga od programisty programowania żadnego niestandardowego zachowania.

  • Gdy część zespołu, nazwa nad postacią gracza Model zostanie skoloryzowana do Team.TeamColor
  • Zmiana Player.TeamColor spowoduje, że Player.Team przełączy się do zespołu z odpowiednim Team.TeamColor
  • Podczas używania domyślnej listy graczy użytkownicy zostaną zgrupowani i wyświetleni razem jako zespół
  • Ustawienie Player.Neutral na prawdę spowoduje, że Player zostanie odłączone od zespołu, ale nie zmieni Player.Team lub Player.TeamColor
  • Kiedy Player dołączy do gry, zostaną przydzielone do zespołu z Team.AutoAssignable ustawionym na prawdę, który ma najmniej graczy.Jeśli nie ma dostępnej drużyny przypisywalnej automatycznie, Player.Neutral zostanie ustawione na prawdę
  • Gdy jest ustawiony na fałsz, tylko gracze, których pasuje do >, mogą się pojawić na tym
  • Gdy zostanie ustawiony na prawdę, łuk gracza zmieni się na gdy jego postać dotknie

Opcjonalne rozszerzone zachowania zespołów Wielu programistów zdecydowało się dodać następujące funkcje do zespołów w własnym kodzie.

  • Wdroż kontrolki w kodzie broni, aby zapobiec przyjacielskiemu ognowi.
  • Wdroż kontrolki w drzwiach lub innych funkcjach, które pozwalają tylko pewnym drużynom ich używać
  • Okresowo zmieniaj zespoły, aby zachować równowagę zespołów

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.

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)

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])

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)

Podsumowanie

Właściwości

  • Odczyt równoległy

    Właściwość ta określa, czy Players zostanie automatycznie umieszczona na Team podczas dołączania.Jeśli wiele zespołów ma tę właściwość ustawioną na prawdę, Roblox spróbuje wyrównać zespoły, gdy Players zostaną dodane.

  • Odczyt równoległy

    Właściwość ta ustawia kolor Team.Określa właściwość Player.TeamColor graczy, którzy są członkami zespołu.Określa również kolor wyświetlany na liście graczy i nad głowami graczy.

Metody

Zdarzenia

Właściwości

AutoAssignable

Odczyt równoległy

Właściwość ta określa, czy Players zostanie automatycznie umieszczona na Team podczas dołączania.Jeśli wiele zespołów ma tę właściwość ustawioną na prawdę, Roblox spróbuje wyrównać zespoły, gdy Players zostaną dodane.

Kiedy Player dołączy do gry, zostaną przypisane do Team z ustawieniem Team.AutoAssignable na prawdę, które ma najmniej graczy.Jeśli nie ma takiej drużyny dostępnej, Player.Neutral zostanie ustawione na prawdę.

Używanie tej właściwości pomoże wyrównać zespoły, gdy gracze zostaną dodani, nie zrobi nic, gdy gracze zostaną usunięci.Z tego powodu programiści mogą chcieć wdrożyć własny system balansowania zespołów.

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.

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)

TeamColor

Odczyt równoległy

Właściwość ta określa kolor Team . Określa właściwość Player.TeamColor graczy, którzy są członkami zespołu.

Wiele domyślnych funkcji zespołu Roblox opiera się na kolorze zespołu, a nie na nazwie lub obiekcie.Na przykład SpawnLocations można przypisać do zespołu za pomocą SpawnLocation.TeamColor .Z tego powodu zaleca się, aby programiści zapewnili, że każdy Team ma unikalny kolor zespołu.

Każdy gracz, który jest częścią zespołu, będzie miał zmieniony kolor nazwy na właściwość TeamColor zespołu.Zostaną one również umieszczone pod nagłówkiem zespołu na liście graczy.

Przykłady kodu

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)

Metody

GetPlayers

Instances
Zapis równoległy

Zwraca listę Players, które są przypisane do Team.A Player jest uważany za przypisany, jeśli ich właściwość Player.Team jest równa Team i Player.Neutral jest fałszywa.

Funkcja ta ma wiele potencjalnych zastosowań, w tym liczenie liczby graczy na Team lub dawanie każdemu Player na Team na Tool .


Zwroty

Instances

Matryca Players w Team.

Przykłady kodu

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

Zdarzenia

PlayerAdded

Wybucha za każdym razem, gdy Player jest przypisywany do Team.Gracz jest uważany za przypisany, jeśli jego właściwość Player.Team jest równa Team i Player.Neutral jest fałszywa.

To wydarzenie jest specyficzne dla zespołu i będzie strzelać tylko wtedy, gdy Player dołączy do specyficznego Team.Każda funkcja połączona z tym wydarzeniem otrzyma obiekt Player gracza, który dołączył do zespołu.Na przykład:

Team.PlayerAdded:Connect(funkcja(gracz) drukuje(gracz.Name.." dołączył do zespołu") kończyć)

Parametry

player: Player

The Player które zostało dodane.


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.

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)

PlayerRemoved

Wybucha za każdym razem, gdy Player zostanie usunięty z Team .Może to być spowodowane opuszczeniem graprzez lub ustawieniem jej na prawdę lub dołączeniem do innej drużyny >.

To wydarzenie jest specyficzne dla zespołu i będzie strzelać tylko wtedy, gdy Player opuści określone Team.Każda funkcja połączona z tym wydarzeniem otrzyma obiekt Player gracza, który opuścił zespół.Na przykład:

Team.PlayerRemoved:Connect(funkcja(gracz) drukuje(player.Name.." opuścił zespół") kończyć)

Parametry

player: Player

Usuwano Player usunięto


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.

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)