Teams
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
Il servizio Team memorizza gli oggetti di un Gioco. Gli oggetti devono essere genitori al servizio Team.
Le squadre offrono una gamma di funzionalità utili per gli sviluppatori.Questi possono essere ampiamente divisi in funzionalità che funzionano out-of-the-box e funzionalità che gli sviluppatori possono programmare nel loro Gioco.
Comportamento della squadra integrato La seguente funzionalità delle squadre esiste di default e non richiede che il programmatore programmi alcun comportamento personalizzato.
- Quando parte di una squadra, il nome sopra il personaggio di un GiocatoreModel sarà colorato al Team.TeamColor
- Cambiare Player.TeamColor cambierà Player.Team il passaggio alla squadra con il corrispondente Team.TeamColor
- Quando si utilizza la lista predefinita dei giocatori, gli utenti verranno gruppati e visualizzati dalla team
- Impostare Player.Neutral a vero causerà il Player di essere dis-associato con il team, ma non cambierà Player.Team o Player.TeamColor
- Quando un Player si unisce a una Gioco, verranno assegnati alla squadra con Team.AutoAssignable impostata su vero che ha il minor numero di giocatori.Se non è disponibile alcuna squadra assegnabile automaticamente, Player.Neutral verrà impostata su vero
- Quando SpawnLocation.Neutral è impostato su false, solo i giocatori i cui Player.TeamColor corrisponde a SpawnLocation.TeamColor possono spawnare su quello SpawnLocation
- Quando SpawnLocation.AllowTeamChangeOnTouch è impostato su vero, il Player.TeamColor di un Giocatorecambierà in SpawnLocation.TeamColor quando il loro personaggio tocca il SpawnLocation
Opzionale comportamento esteso della squadra Molti sviluppatori hanno scelto di aggiungere le seguenti funzionalità alle squadre nel proprio codice.
- Implementa controlli per il team nel codice dell'arma per prevenire l'uccisione del team
- Implementa porte o altre funzionalità che solo alcune squadre possono utilizzare
- Riassegnare periodicamente le squadre per mantenere l'saldodella 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.
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])
Sommario
Proprietà
Metodi
Proprietà
Metodi
GetTeams
La funzione GetTeam restituisce una tabella che contiene gli oggetti del GiocoTeam .
Nota che questo restituirà solo oggetti Team che sono direttamente parented al servizio Teams.Per questo motivo si consiglia agli sviluppatori di genitori solo Team oggetti al servizio Teams e non ad altri Instances (o l'uno all'altro).
Restituzioni
Un array di Teams nel Gioco.
Campioni di codice
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