Team
*Este contenido se traduce usando la IA (Beta) y puede contener errores. Para ver esta página en inglés, haz clic en aquí.
La clase Team representa una facción en un lugar de Roblox.El único padre válido para un equipo está en el servicio Teams.Los equipos ofrecen una serie de funciones útiles para los desarrolladores que se pueden dividir en dos grupos aproximados:
- Características que funcionan 'fuera de la caja delimitadora'
- Características que los desarrolladores pueden programar en su juego.
Comportamiento del equipo integrado La siguiente funcionalidad de equipos existe por defecto y no requiere que el desarrollador programe ningún comportamiento personalizado.
- Cuando parte de un equipo, el nombre sobre el personaje de un jugador Model se coloreará al Team.TeamColor
- Cambiar Player.TeamColor hará que Player.Team cambie al equipo con el correspondiente Team.TeamColor
- Al usar la lista de jugadores predeterminada, los usuarios se agruparán y se mostrarán juntos como un equipo
- Establecer Player.Neutral a verdad hará que el Player se desasocie con el equipo, pero no cambiará Player.Team o Player.TeamColor
- Cuando un Player se una a un juego, se asignarán al equipo con Team.AutoAssignable establecido en verdad que tenga el menor número de jugadores.Si no hay un equipo asignable automáticamente disponible, Player.Neutral se establecerá en verdadero
- Cuando SpawnLocation.Neutral está configurado como falso, solo los jugadores cuyas coincidencias de Player.TeamColor coincidan con SpawnLocation.TeamColor pueden aparecer en ese SpawnLocation
- Cuando se establece en verdadero, el de un jugador cambiará a cuando su personaje toque el
Comportamientos de equipo extendido opcionales Muchos desarrolladores eligieron agregar las siguientes características a los equipos en su propio código.
- Implementa verificaciones en el código de armas para prevenir el desencadenaramigo.
- Implementa controles en puertas u otras características que solo permiten que ciertos equipos los utilicen
- Reasignar periódicamente equipos para mantener el saldode equipos
Muestras de código
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)
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])
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)
Resumen
Propiedades
Esta propiedad determina si Players se colocará automáticamente en el Team al unirse.Si varios equipos tienen esta propiedad establecida en verdad, Roblox intentará igualar los equipos cuando se agreguen Players.
Esta propiedad establece el color del Team .Determina la propiedad Player.TeamColor de los jugadores que son miembros del equipo.También determina el color que se muestra en la lista de jugadores y sobre las cabezas de los jugadores.
Métodos
Devuelve una lista de Players que se asignan al Team.Un Player se considera asignado si su propiedad Player.Team es igual a la de Team y Player.Neutral es falsa.
Eventos
Se activa cada vez que se asigna un Player a la Team.Un jugador se considera asignado si su propiedad Player.Team es igual a la de Team y Player.Neutral es falsa.
Se incendia cada vez que se elimina un Player de un Team.Esto puede deberse a que el Player deja el juego, Player.Neutral se establece en verdadero o el Player se une a un equipo diferente.
Propiedades
AutoAssignable
Esta propiedad determina si Players se colocará automáticamente en el Team al unirse.Si varios equipos tienen esta propiedad establecida en verdad, Roblox intentará igualar los equipos cuando se agreguen Players.
Cuando un Player se una a un juego, se le asignará al Team con Team.AutoAssignable establecido en verdad que tenga el menor número de jugadores.Si no hay tal equipo disponible, Player.Neutral se establecerá en verdadero.
Tenga en cuenta que al usar esta propiedad ayudará a equilibrar los equipos cuando se agreguen jugadores, no hará nada cuando se eliminen jugadores.Por esta razón, los desarrolladores pueden desear implementar su propio sistema de equilibrio de equipos.
Muestras de código
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)
TeamColor
Esta propiedad establece el color del Team . Determina la propiedad Player.TeamColor de los jugadores que son miembros del equipo.
Mucha de la funcionalidad predeterminada del equipo de Roblox se basa en el color del equipo, en lugar del nombre o del objeto.Por ejemplo, SpawnLocations se puede asignar a un equipo a través de SpawnLocation.TeamColor .Por esta razón, se recomienda que los desarrolladores aseguren que cada Team tenga un color de equipo único.
Cualquier jugador que sea parte de un equipo cambiará el color de su nombre a la propiedad de color de equipo del equipo.También se colocarán debajo de la cabecera del equipo en la lista de jugadores.
Muestras de código
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)
Métodos
GetPlayers
Devuelve una lista de Players que se asignan al Team.Un Player se considera asignado si su propiedad Player.Team es igual a la de Team y Player.Neutral es falsa.
Esta función tiene una serie de usos potenciales, incluido contar el número de jugadores en un Team o dar a cada Player en un Team un Tool .
Devuelve
Muestras de código
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
Eventos
PlayerAdded
Se activa cada vez que se asigna un Player a la Team.Un jugador se considera asignado si su propiedad Player.Team es igual a la de Team y Player.Neutral es falsa.
Este evento es específico del equipo y solo se disparará cuando un Player se una al específico Team.Cualquier función conectada a este evento se pasará el objeto Player del jugador que se unió al equipo.Por ejemplo:
Team.PlayerAdded:Connect(función (jugador) imprime (nombre del jugador.." se ha unido al equipo") finalizar)
Parámetros
Muestras de código
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)
PlayerRemoved
Se incendia cada vez que se elimina un Player de un Team.Esto puede deberse a que el Player deja el juego, Player.Neutral se establece en verdadero o el Player se une a un equipo diferente.
Este evento es específico del equipo y solo se disparará cuando un Player salga del específico Team.Cualquier función conectada a este evento se pasará el objeto Player del jugador que dejó el equipo.Por ejemplo:
Team.PlayerRemoved:Connect(función (jugador) imprime (el nombre del jugador.." ha dejado el equipo") finalizar)
Parámetros
Muestras de código
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)