GroupService
*Este contenido se traduce usando la IA (Beta) y puede contener errores. Para ver esta página en inglés, haz clic en aquí.
GroupService es un servicio que permite a los desarrolladores recuperar información sobre un grupo de Roblox desde dentro de un juego.
La información básica sobre el grupo, incluido su nombre, descripción, propietario, roles y emblema se puede recuperar usando GroupService:GetGroupInfoAsync() .Las listas de aliados y enemigos de un grupo se pueden recuperar usando GroupService:GetAlliesAsync() y GroupService:GetEnemiesAsync().
GroupService también se puede usar para recuperar una lista de grupos a los que un jugador es miembro, usando GroupService:GetGroupsAsync() .Tenga en cuenta, los desarrolladores que deseen verificar si un jugador está en un grupo deberían usar la función en lugar de >.
El servicio tiene una serie de aplicaciones útiles, como detectar si un jugador es aliado o enemigo al unirse al juego.
Muestras de código
This code sample demonstrates how GroupService and Player:IsInGroup() can be used to determine whether a player is a member of a group, or any of its allies or enemies.
Note as GroupService:GetAlliesAsync() and GroupService:GetEnemiesAsync() use StandardPages objects a utility function is used to convert them to allies.
local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
-- define group id here
local GROUP_ID = 271454
-- utility function for dealing with pages
local function pagesToArray(pages)
local array = {}
while true do
for _, v in ipairs(pages:GetCurrentPage()) do
table.insert(array, v)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
return array
end
-- get lists of allies and enemies
local alliesPages = GroupService:GetAlliesAsync(GROUP_ID)
local enemiesPages = GroupService:GetEnemiesAsync(GROUP_ID)
-- convert to array
local allies = pagesToArray(alliesPages)
local enemies = pagesToArray(enemiesPages)
local function playerAdded(player)
-- check to see if the player is in the group
if player:IsInGroup(GROUP_ID) then
print(player.Name .. " is a member!")
else
local isAlly, isEnemy = false, false
-- check to see if the player is in any ally groups
for _, groupInfo in ipairs(allies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isAlly = true
break
end
end
-- check to see if the player is in any enemy groups
for _, groupInfo in ipairs(enemies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isEnemy = true
break
end
end
if isAlly and not isEnemy then
print(player.Name .. " is an ally!")
elseif isEnemy and not isAlly then
print(player.Name .. " is an enemy!")
elseif isEnemy and isAlly then
print(player.Name .. " is both an ally and an enemy!")
else
print(player.Name .. " is neither an ally or an enemy!")
end
end
end
-- listen for new players being added
Players.PlayerAdded:Connect(playerAdded)
-- handle players already in game
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
Resumen
Métodos
Devuelve un objeto StandardPages que incluye información sobre todos los aliados del grupo especificado.
Devuelve un objeto StandardPages que incluye información sobre todos los enemigos del grupo especificado.
Devuelve una tabla que contiene información sobre el grupo dado.
Devuelve una lista de tablas que contienen información sobre todos los grupos a los que un jugador dado es miembro.
Propiedades
Métodos
GetAlliesAsync
Devuelve un objeto StandardPages que incluye información sobre todos los aliados del grupo especificado.
Estas páginas no incluyen una lista de ID de grupo sino una lista de tablas de información de grupo, que reflejan el formato de las devueltas por GroupService:GetGroupInfoAsync() .Vea a continuación la estructura de estas tablas.
group = {Name = "Knights of the Seventh Sanctum",Id = 377251,Owner = {Name = "Vilicus",Id = 23415609},EmblemUrl = "http://www.roblox.com/asset/?id=60428602",Description = "We fight alongside the balance to make sure no one becomes to powerful",Roles = {[1] = {Name = "Apprentice",Rank = 1},[2] = {Name = "Warrior",Rank = 2},[3] = {Name = "Earth Walker",Rank = 255}}}
Tenga en cuenta que, ya que esta función devuelve un objeto StandardPages en lugar de un matriz/lista, los desarrolladores pueden desear convertirlo a un array para facilitar su uso (ver ejemplos).
Esta función tiene una serie de aplicaciones útiles, incluyendo detectar si un jugador es miembro de un grupo aliado.
Para los enemigos, usa GroupService:GetEnemiesAsync() .
Parámetros
El ID del grupo.
Devuelve
Muestras de código
local Players = game:GetService("Players")
local GroupService = game:GetService("GroupService")
local GROUP_ID = 57
-- creates a table of all of the allies of a given group
local allies = {}
local pages = GroupService:GetAlliesAsync(GROUP_ID)
while true do
for _, group in pairs(pages:GetCurrentPage()) do
table.insert(allies, group)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
function onPlayerAdded(player)
for _, group in pairs(allies) do
if player:IsInGroup(group.Id) then
print("Player is an ally!")
break
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- handle players who joined while the allies list was still loading
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
This code sample demonstrates how GroupService and Player:IsInGroup() can be used to determine whether a player is a member of a group, or any of its allies or enemies.
Note as GroupService:GetAlliesAsync() and GroupService:GetEnemiesAsync() use StandardPages objects a utility function is used to convert them to allies.
local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
-- define group id here
local GROUP_ID = 271454
-- utility function for dealing with pages
local function pagesToArray(pages)
local array = {}
while true do
for _, v in ipairs(pages:GetCurrentPage()) do
table.insert(array, v)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
return array
end
-- get lists of allies and enemies
local alliesPages = GroupService:GetAlliesAsync(GROUP_ID)
local enemiesPages = GroupService:GetEnemiesAsync(GROUP_ID)
-- convert to array
local allies = pagesToArray(alliesPages)
local enemies = pagesToArray(enemiesPages)
local function playerAdded(player)
-- check to see if the player is in the group
if player:IsInGroup(GROUP_ID) then
print(player.Name .. " is a member!")
else
local isAlly, isEnemy = false, false
-- check to see if the player is in any ally groups
for _, groupInfo in ipairs(allies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isAlly = true
break
end
end
-- check to see if the player is in any enemy groups
for _, groupInfo in ipairs(enemies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isEnemy = true
break
end
end
if isAlly and not isEnemy then
print(player.Name .. " is an ally!")
elseif isEnemy and not isAlly then
print(player.Name .. " is an enemy!")
elseif isEnemy and isAlly then
print(player.Name .. " is both an ally and an enemy!")
else
print(player.Name .. " is neither an ally or an enemy!")
end
end
end
-- listen for new players being added
Players.PlayerAdded:Connect(playerAdded)
-- handle players already in game
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
GetEnemiesAsync
Devuelve un objeto StandardPages que incluye información sobre todos los enemigos del grupo especificado.
Estas páginas no incluyen una lista de ID de grupo sino una lista de tablas de información de grupo, que reflejan el formato de las devueltas por GroupService:GetGroupInfoAsync() .Vea a continuación la estructura de estas tablas.
group = {Name = "Knights of the Seventh Sanctum",Id = 377251,Owner = {Name = "Vilicus",Id = 23415609},EmblemUrl = "http://www.roblox.com/asset/?id=60428602",Description = "We fight alongside the balance to make sure no one becomes to powerful",Roles = {[1] = {Name = "Apprentice",Rank = 1},[2] = {Name = "Warrior",Rank = 2},[3] = {Name = "Earth Walker",Rank = 255}}}
Tenga en cuenta que, ya que esta función devuelve un objeto StandardPages en lugar de un matriz/lista, los desarrolladores pueden desear convertirlo a un array para facilitar su uso (ver ejemplos).
Esta función tiene una serie de aplicaciones útiles, incluyendo detectar si un jugador es miembro de un grupo enemigo.
Para aliados, utilice GroupService:GetAlliesAsync() .
Parámetros
El ID del grupo.
Devuelve
Muestras de código
local Players = game:GetService("Players")
local GroupService = game:GetService("GroupService")
local GROUP_ID = 57
-- creates a list of all of the enemies of a given group
local enemies = {}
local pages = GroupService:GetEnemiesAsync(GROUP_ID)
while true do
for _, group in pairs(pages:GetCurrentPage()) do
table.insert(enemies, group)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
function onPlayerAdded(player)
for _, enemyGroup in pairs(enemies) do
if player:IsInGroup(enemyGroup.Id) then
print("Player is an enemy!")
break
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- handle players who joined while the enemies list was still loading
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
This code sample demonstrates how GroupService and Player:IsInGroup() can be used to determine whether a player is a member of a group, or any of its allies or enemies.
Note as GroupService:GetAlliesAsync() and GroupService:GetEnemiesAsync() use StandardPages objects a utility function is used to convert them to allies.
local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
-- define group id here
local GROUP_ID = 271454
-- utility function for dealing with pages
local function pagesToArray(pages)
local array = {}
while true do
for _, v in ipairs(pages:GetCurrentPage()) do
table.insert(array, v)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
return array
end
-- get lists of allies and enemies
local alliesPages = GroupService:GetAlliesAsync(GROUP_ID)
local enemiesPages = GroupService:GetEnemiesAsync(GROUP_ID)
-- convert to array
local allies = pagesToArray(alliesPages)
local enemies = pagesToArray(enemiesPages)
local function playerAdded(player)
-- check to see if the player is in the group
if player:IsInGroup(GROUP_ID) then
print(player.Name .. " is a member!")
else
local isAlly, isEnemy = false, false
-- check to see if the player is in any ally groups
for _, groupInfo in ipairs(allies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isAlly = true
break
end
end
-- check to see if the player is in any enemy groups
for _, groupInfo in ipairs(enemies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isEnemy = true
break
end
end
if isAlly and not isEnemy then
print(player.Name .. " is an ally!")
elseif isEnemy and not isAlly then
print(player.Name .. " is an enemy!")
elseif isEnemy and isAlly then
print(player.Name .. " is both an ally and an enemy!")
else
print(player.Name .. " is neither an ally or an enemy!")
end
end
end
-- listen for new players being added
Players.PlayerAdded:Connect(playerAdded)
-- handle players already in game
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
GetGroupInfoAsync
Devuelve una tabla que contiene información sobre el grupo dado.
La tabla devuelta es del mismo formato que la devuelta en GroupService:GetAlliesAsync() y GroupService:GetEnemiesAsync(). Este formato se puede ver a continuación.
group = {Name = "Knights of the Seventh Sanctum",Id = 377251,Owner = {Name = "Vilicus",Id = 23415609},EmblemUrl = "http://www.roblox.com/asset/?id=60428602",Description = "We fight alongside the balance to make sure no one becomes to powerful",Roles = {[1] = {Name = "Apprentice",Rank = 1},[2] = {Name = "Warrior",Rank = 2},[3] = {Name = "Earth Walker",Rank = 255}}}
Tenga en cuenta, si un grupo no tiene dueño, el campo Dueño se establecerá en nil.
Esta función tiene una serie de aplicaciones útiles, incluida la carga de la última descripción y logo de un grupo para su visualización en una base de grupo.
Parámetros
El ID de grupo del grupo.
Devuelve
Un diccionario de información sobre el grupo.
Muestras de código
local GroupService = game:GetService("GroupService")
local GROUP_ID = 377251
local group = GroupService:GetGroupInfoAsync(GROUP_ID)
print(group.Name .. " has the following roles:")
for _, role in ipairs(group.Roles) do
print("Rank " .. role.Rank .. ": " .. role.Name)
end
The code in this sample spawns a Part in the Workspace that includes a texture of the given group's emblem.
local GroupService = game:GetService("GroupService")
local function getEmblemAsync(groupId)
local groupInfo = GroupService:GetGroupInfoAsync(groupId)
return groupInfo.EmblemUrl
end
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(5, 5, 1)
part.Position = Vector3.new(0, 5, 0)
local decal = Instance.new("Decal")
decal.Parent = part
part.Parent = workspace
decal.Texture = getEmblemAsync(377251)
GetGroupsAsync
Atención/advertencia: La propiedad IsInClan en la tabla devuelta siempre devolverá falso y existe para la compatibilidad con versiones anteriores.La función de clanes se retiró de la plataforma Roblox en 2016.
Esta función devuelve una lista de tablas que contienen información sobre todos los grupos a los que un dado Player es miembro.
La lista devuelta incluirá una entrada para cada grupo al que el jugador pertenece. Estas entradas son tablas con los siguientes campos.
<th>Descripción</th></tr></thead><tbody><tr><td><b>Nombre</b></td><td>El nombre del grupo</td></tr><tr><td><b>Id</b></td><td>El ID del grupo</td></tr><tr><td><b>EmblemUrl</b></td><td>Un enlace de URL que apunta a la miniatura del grupo (por ejemplo: http://www.roblox.com/asset/?id=276165514)</td></tr><tr><td><b>EmblemId</b></td><td>El assetId del emblema, el mismo que se usa en la URL del emblema</td></tr><tr><td><b>Rango</b></td><td>El rankId que tiene el jugador (por ejemplo: 255 para el propietario)</td></tr><tr><td><b>Rol</b></td><td>El nombre del rango de grupo del jugador (por ejemplo: Propietario del grupo)</td></tr><tr><td><b>Es primario</b></td><td>Un booleano que indica si este es el grupo principal del jugador</td></tr><tr><td><b>Está en el clan</b></td><td>Un booleano que indica si el jugador está en el clan de este grupo</td></tr></tbody>
Nombre |
---|
A diferencia de GroupService:GetAlliesAsync() y GroupService:GetEnemiesAsync(), GetGroupsAsync devuelve una tabla en lugar de un objeto StandardPages.
Parámetros
El Player.UserId del usuario.
Devuelve
Muestras de código
This code sample will print information on all of the groups a player is a member of when they join the game, using GroupService:GetGroupsAsync().
local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
local function playerAdded(player)
-- load a list of info on all groups the player is a member of
local groups = GroupService:GetGroupsAsync(player.UserId)
for _, groupInfo in pairs(groups) do
for key, value in pairs(groupInfo) do
print(key .. ": " .. tostring(value))
end
print("--")
end
end
Players.PlayerAdded:Connect(playerAdded)
-- go through existing players
for _, player in pairs(Players:GetPlayers()) do
playerAdded(player)
end