Team

Visualizza obsoleti

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

La classe Team rappresenta una fazione in un luogo Roblox.L'unico genitore valido per una squadra è nel servizio Teams .Le squadre offrono una gamma di funzionalità utili per gli sviluppatori che possono essere divisi in due gruppi grezzi:

  • Caratteristiche che funzionano "fuori dalla scatola"
  • Le funzionalità degli sviluppatori possono programmare nel loro gioco.

Comportamento della squadra integrata La seguente funzionalità delle squadre esiste per impostazione predefinita e non richiede che il programmatore programmi alcun comportamento personalizzato.

Comportamenti estesi della squadra opzionale Molti sviluppatori hanno scelto di aggiungere le seguenti funzionalità alle squadre nel proprio codice.

  • Implementa controlli nel codice dell'arma per prevenire il fuoco amico.
  • Implementa controlli nelle porte o in altre funzionalità che consentono l'uso solo a determinate squadre
  • Riassegnare periodicamente le squadre per mantenere l'equilibrio della squadra

Campioni di codice

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)

This code sample demonstrates how to assign players to Teams based on their Player.PartyId using the SocialService:GetPlayersByPartyId() method. When a player joins the experience, the script checks whether they're part of a party. If so, it attempts to assign them to the same team as other party members. If no match is found or the player isn't in a party, they're placed on the team with fewer members to maintain balance.

Team Rebalance with Party Integration

local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local SocialService = game:GetService("SocialService")
local redTeam = Instance.new("Team")
redTeam.Name = "Red Team"
redTeam.TeamColor = BrickColor.Red()
redTeam.AutoAssignable = false
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.Name = "Blue Team"
blueTeam.TeamColor = BrickColor.Blue()
blueTeam.AutoAssignable = false
blueTeam.Parent = Teams
local function assignPlayerToTeam(player)
local partyId = player.PartyId
if partyId == "" then
assignBalancedTeam(player)
return
end
local teams = { redTeam, blueTeam }
local matchingTeam = nil
local partyPlayers = SocialService:GetPlayersByPartyId(partyId)
for _, partyPlayer in partyPlayers do
if partyPlayer ~= player and table.find(teams, partyPlayer.Team) then
matchingTeam = partyPlayer.Team
break
end
end
if matchingTeam then
player.Team = matchingTeam
player.TeamColor = matchingTeam.TeamColor
else
assignBalancedTeam(player)
end
end
function assignBalancedTeam(player)
local redCount = #redTeam:GetPlayers()
local blueCount = #blueTeam:GetPlayers()
local chosenTeam = redCount <= blueCount and redTeam or blueTeam
player.Team = chosenTeam
player.TeamColor = chosenTeam.TeamColor
end
local function groupPlayersByParty()
local seenPartyIds = {}
local groupedPlayers = {}
for _, player in Players:GetPlayers() do
if player.PartyId == "" then
table.insert(groupedPlayers, { player })
elseif not table.find(seenPartyIds, player.PartyId) then
table.insert(seenPartyIds, player.PartyId)
local partyPlayers = SocialService:GetPlayersByPartyId(player.PartyId)
table.insert(groupedPlayers, partyPlayers)
end
end
return groupedPlayers
end
-- Call this function to rebalance teams
local function rebalanceTeams()
local groups = groupPlayersByParty()
table.sort(groups, function(a, b)
return #a > #b
end)
for _, player in Players:GetPlayers() do
player.Team = nil
end
for _, group in groups do
local redCount = #redTeam:GetPlayers()
local blueCount = #blueTeam:GetPlayers()
local bestTeam
if redCount <= blueCount then
bestTeam = redTeam
else
bestTeam = blueTeam
end
for _, player in group do
player.Team = bestTeam
player.TeamColor = bestTeam.TeamColor
end
end
for _, player in Players:GetPlayers() do
player:LoadCharacter()
end
end
Players.PlayerAdded:Connect(function(player)
assignPlayerToTeam(player)
player:LoadCharacter()
player:GetPropertyChangedSignal("PartyId"):Connect(function()
if player.PartyId ~= "" then
assignPlayerToTeam(player)
end
end)
end)

Sommario

Proprietà

  • Lettura Parallela

    Questa proprietà determina se Players verrà automaticamente posizionata sul Team quando si unisce.Se più squadre hanno questa proprietà impostata su vero, Roblox cercherà di bilanciare le squadre quando Players vengono aggiunte.

  • Lettura Parallela

    Questa proprietà imposta il colore del Team .Determina la proprietà Player.TeamColor dei giocatori che sono membri della squadra.Determina anche il colore visualizzato nella lista dei giocatori e sopra le teste dei giocatori.

Metodi

Eventi

Proprietà

AutoAssignable

Lettura Parallela

Questa proprietà determina se Players verrà automaticamente posizionata sul Team quando si unisce.Se più squadre hanno questa proprietà impostata su vero, Roblox cercherà di bilanciare le squadre quando Players vengono aggiunte.

Quando un Player si unisce a un gioco verranno assegnati al Team con Team.AutoAssignable impostato su vero che ha il minor numero di giocatori.Se non è disponibile alcuna tale squadra, Player.Neutral verrà impostata su vero.

Nota mentre usi questa proprietà aiuterà a bilanciare le squadre quando i giocatori vengono aggiunti, non farà nulla quando i giocatori vengono rimossi.Per questo motivo gli sviluppatori potrebbero voler implementare il proprio sistema di bilanciamento delle squadre.

Campioni di codice

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

Lettura Parallela

Questa proprietà imposta il colore del Team . Determina la proprietà Player.TeamColor dei giocatori che sono membri della squadra.

Molte funzionalità predefinite del team di Roblox sono basate sul colore del team, piuttosto che sul nome o sull'oggetto.Ad esempio, SpawnLocations può essere assegnato a una squadra attraverso SpawnLocation.TeamColor .Per questo motivo si consiglia agli sviluppatori di garantire che ogni Team abbia un TeamColor unico.

Qualsiasi giocatore che fa parte di una squadra avrà il proprio nome cambiato a TeamColor della squadra.Verranno anche messi sotto la testa del team nella lista dei giocatori.

Campioni di codice

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)

Metodi

GetPlayers

Instances
Scrivi Parallelo

Restituisce una lista di Players che sono assegnati al Team .Un Player è considerato assegnato se la loro proprietà Player.Team è uguale alla Team e Player.Neutral è falsa.

Questa funzione ha una serie di potenziali usi, tra cui il conteggio del numero di giocatori su un Team o dare ogni Player su un Team a Tool .


Restituzioni

Instances

Un array di Players in Team.

Campioni di codice

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

Eventi

PlayerAdded

Si attiva ogni volta che viene assegnato un Player a Team.Un giocatore è considerato assegnato se la sua proprietà Player.Team è uguale a Team e Player.Neutral è falsa.

Questo evento è specifico per il team e verrà attivato solo quando un Player si unisce al specifico Team.Qualunque funzione connessa a questo evento verrà passata l'oggetto Player del giocatore che si è unito alla squadra.Ad esempio:

Team.PlayerAdded:Connect(funzione(player) print(player.Name.." si è unito alla squadra") fine)

Parametri

player: Player

Il Player che è stato aggiunto.


Campioni di codice

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

Si attiva ogni volta che un Player viene rimosso da un Team .Questo può essere dovuto al fatto che Player lascia il gioco, Player.Neutral viene impostato su vero o il Player si unisce a un team diverso.

Questo evento è specifico per il team e verrà attivato solo quando un Player lascia lo specifico Team.Qualunque funzione connessa a questo evento verrà passata l'oggetto Player del giocatore che ha lasciato la squadra.Ad esempio:

Team.PlayerRemoved:Connect(funzione(player) print(player.Name.." ha lasciato la squadra") fine)

Parametri

player: Player

Il Player rimosso.


Campioni di codice

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)