BadgeService

Visualizza obsoleti

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

Non costruibile
Assistenza

La classe BadgeService fornisce informazioni e funzionalità relative ai badge . I badge vengono utilizzati su tutta la piattaforma per riconoscere i risultati e l'attività di un Giocatore. Al concedere un badge a un Giocatore, viene aggiunto al loro inventario e visualizzato nella sua pagina profilo.

Campioni di codice

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

Sommario

Metodi

Proprietà

Metodi

AwardBadge

Resa

Fornisce a Player un badge con il UserId e l'ID del badge. Limite di rate: 50 + 35 * [number di utenti] per minuto *. Per assegnare con successo un badge:

  • Il giocatore deve essere attualmente connesso al Gioco.
  • Il giocatore non deve già avere il badge (nota che un giocatore può eliminare un badge ottenuto dal loro profilo e ottenere il badge nuovamente).
  • Il badge deve essere assegnato da un lato server Script o da un ModuleScript eventualmente richiesto da un Script , non da un 1> Class.LocalScript1> .
  • Il badge deve essere assegnato in un luogo che fa parte del gioco associato al badge.
  • Il badge deve essere abilitato; controlla questo utilizzando la proprietà IsEnabled del badge ottenuto tramite BadgeService:GetBadgeInfoAsync() .

Vedi anche:

Parametri

userId: number

Il Player.UserId dell'utente a cui verrà assegnato il badge.

badgeId: number

L'ID del badge da assegnare.


Restituzioni

Booleano di true se il badge è stato assegnato con successo.

Campioni di codice

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

Resa

Controlla un elenco di ID del badge contro un UserId e restituisce un elenco di ID del badge che il giocatore possiede.

Questo metodo supporta le raffiche di fino a 10 badge. Usa BadgeService:UserHasBadgeAsync() per le ricerche di badge singoli.

Limite di rate: 10+5* [number of players] per minuto in ciascun Server.

Qualsiasi set di badge per qualsiasi esperienza può essere richiesto, indipendentemente dal creatore dei badge o da quale esperienza siano. Tutti i UserId possono essere utilizzati in un Script , ma in un LocalScript , solo il 2> Class.Player.UserId|userId2>

Parametri

userId: number

Il UserId del giocatore per controllare la proprietà di eventuali badge specificati.

badgeIds: Array

La lista di ID delle insignie per controllare la proprietà. Lunghezza massima di 10.


Restituzioni

La lista di ID del badge che l'utente ha ottenuto presso i badge ID forniti. Vuoto se nessuno dei badge forniti è di proprietà dell'utente. Non garantito che sia nello stesso ordine come la lista di input.

Campioni di codice

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

Resa

Questa funzione ottiene informazioni su un badge dato il suo ID. Ci vuole un breve momento per caricare le informazioni dal sito Web Roblox; le chiamate ripetute memorizzeranno per un breve periodo. Ritorna un dizionario con i seguenti campi:


<tbody>
<tr>
<td><b>Nome</b></td>
<td>stringa</td>
<td>Il nome del badge.</td>
</tr>
<tr>
<td><b>Descrizione</b></td>
<td>stringa</td>
<td>La descrizione del badge.</td>
</tr>
<tr>
<td><b>IconImageId</b></td>
<td>int64</td>
<td>L'ID risorsa dell'immagine per il badge.</td>
</tr>
<tr>
<td><b>È abilitato</b></td>
<td>booleano</td>
<td>Indica se il badge è disponibile per essere assegnato.</td>
</tr>
</tbody>
ChiaveTipoDescrizione

Vedi anche:

Parametri

badgeId: number

L'ID del badge della persona che le informazioni dovrebbero essere prese.


Restituzioni

Un dizionario di informazioni sul badge specificato.

Campioni di codice

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

Resa

Controlla e restituisce se un Player possiede un badge dato dal loro Class.Player.UserId|UserId</

Qualsiasi badge per qualsiasi gioco può essere richiesto, non importa chi ha creato il badge o per quale esperienza è stato utilizzato.

Vedi anche:

Parametri

userId: number

Il Player.UserId del giocatore per controllare la proprietà del badge specificato.

badgeId: number

L'ID del badge della badge che sarà controllata la sua proprietà.


Restituzioni

Indica se l'utente specificato ha il badge specificato.

Campioni di codice

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)

Eventi