BadgeService

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

La classe BadgeService fournit des informations et des fonctionnalités relatives aux badges .Les badges sont utilisés sur la plate-forme pour reconnaître les réalisations et l'activité d'un joueur.Lorsqu'un badge est attribué à un joueur, il est ajouté à son inventaire et affiché sur sa page de profil.

Échantillons de code

The following example creates an awardBadge() function that handles potential errors that may occur when awarding a badge. Using properties of the badge fetched via BadgeService:GetBadgeInfoAsync(), it confirms that the badge can be awarded and does so using BadgeService:AwardBadge().

Awarding a Badge

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 0
local function awardBadge(player, badgeId)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awardSuccess then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
else
warn("Error while fetching badge info: " .. badgeInfo)
end
end
local function onPlayerAdded(player)
awardBadge(player, BADGE_ID)
end
Players.PlayerAdded:Connect(onPlayerAdded)

The following script waits for any player to enter the game and checks if they own a specific badge. This is useful for creating a restricted area with collision filtering or teleportation that only works if a player owns a special badge.

Checking Earned Badges

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 00000000 -- Change this to your badge ID
local function onPlayerAdded(player)
-- Check if the player has the badge
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
-- Handle player's badge ownership as needed
print(player.Name, "has badge", badgeId)
end
end
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)

This script checks which badges a user owns when they join the experience.

Checking Earned Badges in Batches

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeIds = { 0000000000, 1111111111, 2222222222 } -- Change this to a list of your badge IDs
local function onPlayerAdded(player)
-- Check if the player has any of the badges
local success, result = pcall(function()
return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player", player.Name, "has badges:", result)
return
end
local ownedBadgeIds = result
if #ownedBadgeIds == 0 then
print(player.Name, "does not have any of the badges")
else
print(player.Name, "has the following badges:", table.concat(ownedBadgeIds, ", "))
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

This sample prints badge information fetched via BadgeService:GetBadgeInfoAsync().

Getting Badge Info

local BadgeService = game:GetService("BadgeService")
-- Fetch badge information
local success, result = pcall(function()
return BadgeService:GetBadgeInfoAsync(00000000) -- Change this to desired badge ID
end)
-- Output the information
if success then
print("Badge:", result.Name)
print("Enabled:", result.IsEnabled)
print("Description:", result.Description)
print("Icon:", "rbxassetid://" .. result.IconImageId)
else
warn("Error while fetching badge info:", result)
end

Résumé

Méthodes

Propriétés

Méthodes

AwardBadge

Rendement

Accorde un badge Player avec le badge ID et le badge UserId.Limite de taux : 50 + 35 * [nombre d'utilisateurs] par minute .Afin d'attribuer avec succès un badge :

  • Le joueur doit être actuellement connecté au jeu.
  • Le joueur ne doit pas déjà avoir le badge (veuillez noter qu'un joueur peut supprimer un badge attribué de son profil et recevoir à nouveau le badge).
  • Le badge doit être attribué d'un côté serveur Script ou d'un ModuleScript finalement requis par un Script , pas d'un LocalScript .
  • Le badge doit être décerné dans un endroit qui fait partie du jeu associé au badge.
  • Le badge doit être activé ; vérifiez cela en utilisant la propriété IsEnabled du badge récupéré via BadgeService:GetBadgeInfoAsync() .

Voir aussi :

Paramètres

userId: number

Le Player.UserId de l'utilisateur auquel le badge doit être attribué.

Valeur par défaut : ""
badgeId: number

L'ID du badge à attribuer.

Valeur par défaut : ""

Retours

Boolean de true si le badge a été attribué avec succès.

Échantillons de code

The following example creates an awardBadge() function that handles potential errors that may occur when awarding a badge. Using properties of the badge fetched via BadgeService:GetBadgeInfoAsync(), it confirms that the badge can be awarded and does so using BadgeService:AwardBadge().

Awarding a Badge

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 0
local function awardBadge(player, badgeId)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awardSuccess then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
else
warn("Error while fetching badge info: " .. badgeInfo)
end
end
local function onPlayerAdded(player)
awardBadge(player, BADGE_ID)
end
Players.PlayerAdded:Connect(onPlayerAdded)

CheckUserBadgesAsync

Rendement

Vérifie une liste d'ID de badge contre un UserId et renvoie une liste d'ID de badge que le joueur possède.

Cette méthode prend en charge des lots de jusqu'à 10 badges. Utilisez BadgeService:UserHasBadgeAsync() pour les recherches de badges individuelles.

Limite de taux : 10 + 5 * [nombre de joueurs] par minute dans chaque serveur.

Tout ensemble de badges pour n'importe quelle expérience peut être interrogé, peu importe qui a créé les badges ou pour quelles expériences ils sont.Toute UserId peut être utilisée dans un Script , mais dans un LocalScript , seule la UserId de l'utilisateur local dont le client exécute le script peut être utilisée.

Paramètres

userId: number

Le UserId du joueur pour vérifier la propriété des badges spécifiés.

Valeur par défaut : ""
badgeIds: Array

La liste des ID des badges à vérifier pour vérifier la propriété. Longueur maximale de 10.

Valeur par défaut : ""

Retours

La liste des ID de badge appartient à l'utilisateur donné en dehors des ID de badge fournis.Vide si aucun des badges fournis n'est possédé par l'utilisateur donné.Non garanti d'être dans le même ordre que la liste d'entrée.

Échantillons de code

This script checks which badges a user owns when they join the experience.

Checking Earned Badges in Batches

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeIds = { 0000000000, 1111111111, 2222222222 } -- Change this to a list of your badge IDs
local function onPlayerAdded(player)
-- Check if the player has any of the badges
local success, result = pcall(function()
return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player", player.Name, "has badges:", result)
return
end
local ownedBadgeIds = result
if #ownedBadgeIds == 0 then
print(player.Name, "does not have any of the badges")
else
print(player.Name, "has the following badges:", table.concat(ownedBadgeIds, ", "))
end
end
Players.PlayerAdded:Connect(onPlayerAdded)

GetBadgeInfoAsync

Rendement

Cette fonction récupère des informations sur un badge donné en raison de son ID.Il faut un court instant pour charger les informations du site Web Roblox ; les appels répétés seront en cache pendant une courte durée.Il renvoie un dictionnaire avec les champs suivants :


<th>Type</th>
<th>Avertissement</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Nom</b></td>
<td>chaîne</td>
<td>Le nom du badge.</td>
</tr>
<tr>
<td><b>Avertissement</b></td>
<td>chaîne</td>
<td>La description du badge.</td>
</tr>
<tr>
<td><b>Id d'image icône</b></td>
<td>int64</td>
<td>L'ID de la ressource de l'image pour le badge.</td>
</tr>
<tr>
<td><b>Est activé</b></td>
<td>bool</td>
<td>Indique si le badge est disponible pour être attribué.</td>
</tr>
</tbody>
Clé

Voir aussi :

Paramètres

badgeId: number

L'ID du badge de la badge dont les informations doivent être récupérées.

Valeur par défaut : ""

Retours

Un dictionnaire d'informations sur le badge spécifié.

Échantillons de code

This sample prints badge information fetched via BadgeService:GetBadgeInfoAsync().

Getting Badge Info

local BadgeService = game:GetService("BadgeService")
-- Fetch badge information
local success, result = pcall(function()
return BadgeService:GetBadgeInfoAsync(00000000) -- Change this to desired badge ID
end)
-- Output the information
if success then
print("Badge:", result.Name)
print("Enabled:", result.IsEnabled)
print("Description:", result.Description)
print("Icon:", "rbxassetid://" .. result.IconImageId)
else
warn("Error while fetching badge info:", result)
end

UserHasBadgeAsync

Rendement

Vérifie et renvoie si un Player possède un badge donné leur UserId et l'ID du badge.Limite de taux : 50 + 35 * [nombre de joueurs] par minute .Vous pouvez appeler la fonction depuis le serveur dans un Script ou ModuleScript finalement requis par un Script , et l'utilisateur en question doit être présent sur le serveur pour que la requête s'lancer.Lors de l'appel de la méthode depuis le client dans un LocalScript , elle ne fonctionne que pour l'utilisateur local dont le client exécute le script.

N'importe quel badge pour n'importe quel jeu peut être interrogé, peu importe qui a créé le badge ou pour quelle expérience il est utilisé.

Voir aussi :

Paramètres

userId: number

Le Player.UserId du joueur pour vérifier la propriété du badge spécifié.

Valeur par défaut : ""
badgeId: number

L'ID du badge dont la propriété sera vérifiée.

Valeur par défaut : ""

Retours

Indique si l'utilisateur spécifié a le badge spécifié.

Échantillons de code

The following script waits for any player to enter the game and checks if they own a specific badge. This is useful for creating a restricted area with collision filtering or teleportation that only works if a player owns a special badge.

Checking Earned Badges

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 00000000 -- Change this to your badge ID
local function onPlayerAdded(player)
-- Check if the player has the badge
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
-- Handle player's badge ownership as needed
print(player.Name, "has badge", badgeId)
end
end
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)

Évènements