GroupService

Afficher les obsolètes

*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.

Création impossible
Service
Non répliqué

GroupService est un service qui permet aux développeurs de récupérer des informations sur un groupe Roblox à partir d'un jeu.

Les informations de base sur le groupe, y compris son nom, sa description, son propriétaire, ses rôles et son emblème peuvent être récupérées en utilisant GroupService:GetGroupInfoAsync() .Les listes des alliés et des ennemis d'un groupe peuvent être récupérées en utilisant GroupService:GetAlliesAsync() et GroupService:GetEnemiesAsync() .

GroupService peut également être utilisé pour récupérer une liste des groupes auxquels un joueur est membre, en utilisant GroupService:GetGroupsAsync() .Notez que les développeurs souhaitant vérifier si un joueur est dans un groupe devraient utiliser la fonction Player:IsInGroup() plutôt que GroupService:GetGroupsAsync().

Le service a un certain nombre d'applications utiles, telles que la détection si un joueur est un allié ou un ennemi lorsqu'il rejoint le jeu.

Échantillons de code

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.

Group Ally/Enemy Checker

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

Résumé

Méthodes

Propriétés

Méthodes

GetAlliesAsync

Rendement

Retourne un objet StandardPages incluant des informations sur tous les alliés du groupe spécifié.

Ces pages ne comprennent pas une liste d'ID de groupe mais plutôt une liste de tables d'informations de groupe, reflétant le format de celles retournées par GroupService:GetGroupInfoAsync().Voir ci-dessous pour la structure de ces tables.


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
}
}
}

Remarque, car cette fonction renvoie un objet StandardPages plutôt qu'un matrice, les développeurs peuvent souhaiter le convertir en tableau pour faciliter son utilisation (voir exemples).

Cette fonction a un certain nombre d'applications utiles, notamment pour détecter si un joueur est membre d'un groupe allié.

Pour les ennemis, utilisez GroupService:GetEnemiesAsync() .

Paramètres

groupId: number

L'ID du groupe.

Valeur par défaut : ""

Retours

Échantillons de code

GroupService:GetAlliesAsync

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.

Group Ally/Enemy Checker

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

Rendement

Retourne un objet StandardPages incluant des informations sur tous les ennemis du groupe spécifié.

Ces pages ne comprennent pas une liste d'ID de groupe mais plutôt une liste de tables d'informations de groupe, reflétant le format de celles retournées par GroupService:GetGroupInfoAsync().Voir ci-dessous pour la structure de ces tables.


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
}
}
}

Remarque, car cette fonction renvoie un objet StandardPages plutôt qu'un matrice, les développeurs peuvent souhaiter le convertir en tableau pour faciliter son utilisation (voir exemples).

Cette fonction a un certain nombre d'applications utiles, notamment pour détecter si un joueur est membre d'un groupe ennemi.

Pour les alliés, utilisez GroupService:GetAlliesAsync() .

Paramètres

groupId: number

L'ID du groupe.

Valeur par défaut : ""

Retours

Échantillons de code

GroupService:GetEnemiesAsync

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.

Group Ally/Enemy Checker

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

Variant
Rendement

Renvoie une table contenant des informations sur le groupe donné.

La table renvoyée est du même format que celle renvoyée dans GroupService:GetAlliesAsync() et GroupService:GetEnemiesAsync(). Ce format peut être vu ci-dessous.


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
}
}
}

Remarque, si un groupe n'a pas de propriétaire, le champ propriétaire sera défini sur nil.

Cette fonction a un certain nombre d'applications utiles, notamment le chargement de la dernière description et du logo d'un groupe pour son affichage dans une base de groupe.

Paramètres

groupId: number

L'ID de groupe du groupe.

Valeur par défaut : ""

Retours

Variant

Un dictionnaire d'informations sur le groupe.

Échantillons de code

GroupService:GetGroupInfoAsync

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.

Load Group 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

Rendement

Avertissement : La propriété IsInClan dans la table renvoyée retournera toujours faux et existe pour la compatibilité ascendante.La fonction des clans a été abandonnée par la plate-forme Roblox en 2016.

Cette fonction renvoie une liste de tables contenant des informations sur tous les groupes auxquels un donné Player est membre.

La liste renvoyée inclura une entrée pour chaque groupe auquel le joueur est membre. Ces entrées sont des tables avec les champs suivants.


<th>Avertissement</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Nom</b></td>
<td>Le nom du groupe</td>
</tr>
<tr>
<td><b>Id</b></td>
<td>L'ID du groupe</td>
</tr>
<tr>
<td><b>Url d'emblème</b></td>
<td>Un lien URL vers une vignette du groupe (par exemple : http://www.roblox.com/asset/?id=276165514)</td>
</tr>
<tr>
<td><b>Id d'emblème</b></td>
<td>L'assetId de l'emblème, le même qui est utilisé dans l'URL d'emblème</td>
</tr>
<tr>
<td><b>Rang</b></td>
<td>Le rankId que le joueur a (par exemple : 255 pour le propriétaire)</td>
</tr>
<tr>
<td><b>Rôle</b></td>
<td>Le nom du rang de groupe du joueur (par exemple : propriétaire de groupe)</td>
</tr>
<tr>
<td><b>Est primaire</b></td>
<td>Un booléen indiquant si c'est le groupe principal du joueur</td>
</tr>
<tr>
<td><b>Est dans le clan</b></td>
<td>Un booléen indiquant si le joueur est dans le clan du groupe</td>
</tr>
</tbody>
Nom

Notez que contrairement à GroupService:GetAlliesAsync() et GroupService:GetEnemiesAsync(), GetGroupsAsync renvoie une table plutôt qu'un objet StandardPages.

Paramètres

userId: number

Le Player.UserId de l'utilisateur.

Valeur par défaut : ""

Retours

Un ensemble de dictionnaires contenant des informations sur le groupe auquel il appartient Player est un membre.

Échantillons de code

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().

Getting the Groups that a User is A Member Of

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

Évènements