BadgeService
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
La classe BadgeService fornisce informazioni e funzionalità relative a badge .I badge vengono utilizzati attraverso la piattaforma per riconoscere le prestazioni e l'attività di un Giocatore.Dopo aver assegnato un badge a un Giocatore, viene aggiunto al suo inventario e visualizzato sulla sua pagina di 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().
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.
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.
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().
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
Proprietà
Metodi
Assegna un badge a un giocatore dato l'ID di ciascuno.
Controlla una lista di ID badge contro un UserId e restituisce una lista di ID badge che il giocatore possiede.
Recupera informazioni su un badge dato il suo ID.
Verifica se un giocatore abbia il badge dato il Player.UserId e l'ID del badge.
Proprietà
Metodi
AwardBadge
Forna un Player un badge con il UserId e l'ID del badge.Limite di tasso: 50 + 35* [number of users] al 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 assegnato dal suo profilo e ricevere nuovamente il badge).
- Il badge deve essere assegnato da un lato server Script o da un ModuleScript alla fine richiesto da un Script , non da un LocalScript .
- 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 recuperato attraverso BadgeService:GetBadgeInfoAsync() .
Vedi anche:
Parametri
Il Player.UserId dell'utente al quale verrà assegnato il badge.
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().
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
Controlla una lista di ID badge contro un UserId e restituisce una lista di ID badge che il giocatore possiede.
Questo metodo supporta batch di fino a 10 badge. Usa BadgeService:UserHasBadgeAsync() per le ricerche di singoli badge.
Limite di tasso: 10 + 5* [number of players] per minuto in ogni Server.
Qualsiasi insieme di badge per qualsiasi esperienza può essere interrogato, indipendentemente da chi abbia creato i badge o per quali esperienze siano.Qualsiasi UserId può essere utilizzato in un Script , ma in un LocalScript , solo il UserId dell'utente locale il cui client esegue lo script può essere utilizzato.
Parametri
Il UserId del giocatore per controllare la proprietà dei badge specificati.
L'elenco degli ID dei badge da controllare per verificare la proprietà. Lunghezza massima di 10.
Restituzioni
L'elenco degli ID del badge che il dato utente possiede al di fuori degli ID del badge forniti.Vuoto se nessuno dei badge forniti è posseduto dall'utente indicato.Non garantito di essere nello stesso ordine dell'elenco di input.
Campioni di codice
This script checks which badges a user owns when they join the experience.
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
Questa funzione recupera informazioni su un badge dato il suo ID.Ci vuole un breve momento per caricare le informazioni dal sito Web di Roblox; le chiamate ripetute verranno memorizzate per una breve durata.Restituisce un dizionario con i seguenti campi:
<th>Tipo</th><th>Descrizione</th></tr></thead><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>Id immagine icona</b></td><td>int64</td><td>L'ID risorsa dell'immagine per il badge.</td></tr><tr><td><b>È abilitato</b></td><td>bool</td><td>Indica se il badge è disponibile per essere assegnato.</td></tr></tbody>
Chiave |
---|
Vedi anche:
Parametri
L'ID del badge della badge le cui informazioni devono essere recuperate.
Restituzioni
Un dizionario di informazioni sul badge specificato.
Campioni di codice
This sample prints badge information fetched via BadgeService:GetBadgeInfoAsync().
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
Verifica e restituisce se un Player possiede un badge dato il loro UserId e l'ID del badge.Limite di tasso: 50 + 35 * [number of players] al minuto .Puoi chiamare la funzione dal server in un Script o ModuleScript alla fine richiesta da un Script , e l'utente in questione deve essere presente nel server per l'Eseguiredella query.Quando si chiama il metodo dal client in un LocalScript , funziona solo per l'utente locale il cui client sta eseguendo lo script.
Qualsiasi badge per qualsiasi gioco può essere interrogato, non importa chi abbia creato il badge o per quale esperienza venga utilizzato.
Vedi anche:
Parametri
Il Player.UserId del giocatore per controllare la proprietà del badge specificato.
L'ID del badge del badge il cui possesso verrà controllato.
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.
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)