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 dostarcza informacje i funkcjonalności związane z odznakami. Odznaki są używane na całej platformie do rozpoznania osiągnięć i aktywnośćgracza. Po przyznaniu odznaki graczowi, jest ona dodana do ich zapasu i wyświetlana na ich stronie profilu.
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
Przyznaj graczowi odznakę z danym jego identyfikatorem.
Sprawdza listę ID znaczków przeciwko UserId i wypisuje listę ID znaczków, które posiada gracz.
Zdobądź informacje o odznakach, które są podane ich ID.
Sprawdza, czy gracz ma otrzymany Player.UserId i ID badżu.
Właściwości
Metody
AwardBadge
Daje Playerowi odznakę z UserId i ID odznaki. Limit kraty: 50 + 35 * [liczba użytkowników] na minutę. Aby uhonorować odznaka:
- Gracz musi być obecnie połączony z gra.
- Gracz nie może jeszcze posiadać odznaki (zauważ, że gracz może usunąć odznakę z profilu i otrzymać odznaki ponownie).
- Odznakę należy przyznać z serwera strony Script lub ModuleScript w końcu wymaganą przez Script , nie z serwera 2>Class.LocalScript2>.
- Odznakę należy przyznać w miejscu, które jest częścią gry związanej z odznaka.
- Odznakę trzeba włączyć; sprawdź to używając właściwości IsEnabled odznaki uzyskanej za pośrednictwem BadgeService:GetBadgeInfoAsync().
Zobacz również:
Parametry
Class.Player.UserId użytkownika, któremu ma być przyznany znчоł.
Tożsamość odznaki, która ma być przyznana.
Zwroty
Boolean of true jeśli otrzymano odznakę z sukcesem.
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ę ID znaczków przeciwko UserId i wypisuje listę ID znaczków, które posiada gracz.
Ten metod wspiera serię do 10 znaczków. Użyj BadgeService:UserHasBadgeAsync() dla pojedynczych poszukiwań znaczków.
Limit stawki: 10 + 5 * [liczba graczy] za minutę na każdym serwerze.
Dowolna seria znaczków dla dowolnego doświadczenia może być zapytana, niezależnie od tego, kto stworzył znaczniki lub dla których doświadczeń są one. Każdy UserId może być używany w Script, ale w LocalScript tylko 2>Class.
Parametry
Class.Player.UserId|userId gracza do sprawdzenia statusu posiadania określonych odznak.
Lista ID znaczków, która może być sprawdzana właściciela. Maksymalna długość to 10.
Zwroty
Lista danych oznaczeń, które posiada użytkownik, pochodząca z dostarczonych danych o znaczeniach. Puste, jeśli żadne z dostarczonych znaczeń nie należy do użytkownika. Nie gwarantuje, że będzie w tej samej kolejności co lista wpisów.
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 przypisanych ID. Nie trwa długo, aby załadować informacje z witryny Roblox; powtarzające się wezwania zapamiętują na krótką chwilę. Wynika z niego słownik z następującymi polami:
<tbody><tr><td><b>Imię</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>IconImageId</b></td><td>int64</td><td>Idź ID zdjęcia dla odznaka.</td></tr><tr><td><b>Włączone</b></td><td>boolean</td><td>Wskazuje, czy odznaka jest dostępna do otrzymania.</td></tr></tbody>
Klucz | Typ | Opis |
---|
Zobacz również:
Parametry
ID znaku identyfikacyjnego znaku, którego informacje należy pobrać.
Zwroty
Dizajn słownika 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 weryfikuje, czy Player posiada przypisany im Class.Player.UserId|UserId
Każde rozszerzenie dla dowolnej gry można sprawdzić, niezależnie od tego, kto stworzył rozszerzenie lub dla którego jest ono używane.
Zobacz również:
Parametry
Class.Player.UserId gracza do sprawdzenia własności określonego odznaka.
ID znaku identyfikacyjnego znaku, którego właścicielem będzie sprawdzany.
Zwroty
Wskazuje, czy określony użytkownik ma określony 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)