BadgeService
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Klasa BadgeService zapewnia informacje i funkcjonalność związane z odznakami.Odznaki są używane na całej platformie, aby rozpoznać osiągnięcia i aktywność gracza.Po przyznaniu odznaki graczowi jest ona dodawana do jego zapasu i wyświetlana na jego profilowej stronie.
Przykłady kodu
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
Podsumowanie
Metody
Nagrodz odznakę graczowi, podając ID każdego z nich.
Sprawdza listę identyfikatorów odznak przeciwko UserId i zwraca listę identyfikatorów odznak, które posiada gracz.
Zbierz informacje o odznace, podając jej ID.
Sprawdza, czy gracz posiada odznakę, biorąc pod uwagę Player.UserId i ID odznaki.
Właściwości
Metody
AwardBadge
Przyznaje Player odznakę z UserId i ID odznaki.Limit stawki: 50 + 35 * [liczba użytkowników] na minutę .Aby pomyślnie przyznać odznaka:
- Gracz musi być obecnie połączony z gra.
- Gracz nie może już posiadać odznakę (zauważ, że gracz może usunąć przyznaną odznakę z profilu i otrzymać odznakę ponownie).
- Odznaka musi zostać przyznana ze strony serwera Script lub z ModuleScript w końcu wymagana przez Script , a nie od LocalScript.
- Odznaka musi zostać przyznana w miejscu, które jest częścią gry związanej z odznaka.
- Odznaka musi być włączona; sprawdź to za pomocą właściwości IsEnabled znacznika pobranego za pomocą BadgeService:GetBadgeInfoAsync() .
Zobacz także:
Parametry
The Player.UserId użytkownika, któremu ma zostać przyznana odznaka.
ID odznaki, która ma być przyznana.
Zwroty
Boolean z true jeśli odznaka została przyznana pomyślnie.
Przykłady kodu
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
Sprawdza listę identyfikatorów odznak przeciwko UserId i zwraca listę identyfikatorów odznak, które posiada gracz.
Ta metoda wspiera serie do 10 odznak. Użyj BadgeService:UserHasBadgeAsync() do pojedynczych wyszukiwań odznak.
Limit stawki: 10 + 5* [liczba graczy] na minutę na każdym serwerze.
Można zapytać o dowolny zestaw odznak dla dowolnego doświadczenia, bez względu na to, kto stworzył odznaki lub do jakich doświadczeń one należą.Każdy UserId może być użyty w Script , ale w LocalScript tylko UserId lokalnego użytkownika, którego klient wykonuje skrypt, może być użyty.
Parametry
Lista identyfikatorów odznak do sprawdzenia własności. Maksymalna długość 10.
Zwroty
Lista ID odznak, które dany użytkownik posiada spośród dostarczonych ID odznak.Puste, jeśli żaden z dostarczonych odznak nie jest posiadany przez danego użytkownika.Nie gwarantuje, że będzie w tym samym porządku co lista wejściowa.
Przykłady kodu
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
Funkcja ta pobiera informacje o odznace, podając jej ID.Potrzeba chwili na załadowanie informacji z witryny Roblox; powtarzane wezwania będą przechowywane przez krótki czas.Zwraca słownik z następującymi polami:
<th>Typ</th><th>Opis</th></tr></thead><tbody><tr><td><b>Nazwa</b></td><td>ciąg</td><td>Nazwa odznaka.</td></tr><tr><td><b>Opis</b></td><td>ciąg</td><td>Opis odznaka.</td></tr><tr><td><b>Id obrazu ikony</b></td><td>int64</td><td>ID zasobu obrazu dla odznaka.</td></tr><tr><td><b>Jest włączone</b></td><td>bool</td><td>Wskazuje, czy odznaka jest dostępna do przyznania.</td></tr></tbody>
Klucz |
---|
Zobacz także:
Parametry
ID odznaki, której informacje należy pobrać.
Zwroty
Słownik informacji o określonym odznaka.
Przykłady kodu
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
Sprawdza i zwraca, czy Player posiada odznakę, biorąc pod uwagę jej UserId i ID odznaki.Limit stawki: 50 + 35 * [liczba graczy] na minutę .Możesz wezwać funkcję z serwera w Script lub ModuleScript ostatecznie wymaganą przez Script, a użytkownik, o którym mowa, musi być obecny na serwerze, aby zapytanie zostało wykonane.Podczas wywoływania metody z klienta w LocalScript działa tylko dla lokalnego użytkownika, którego klient wykonuje skrypt.
Można zapytać o dowolną odznakę dla dowolnej gry, bez względu na to, kto stworzył odznakę lub do jakiego doświadczenia jest używana.
Zobacz także:
Parametry
The Player.UserId of the player to check for ownership of the specified odznaka.
ID odznaki, której własność zostanie sprawdzona.
Zwroty
Wskazuje, czy określony użytkownik posiada określoną odznaka.
Przykłady kodu
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)