徽章服務 類提供與徽章相關的資訊和功能。徽章在平台上使用,用於認識玩家的成就和活動。授予玩家徽章後,它將被添加到他們的庫存中,並在他們的個人資料頁面上顯示。
範例程式碼
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
概要
方法
給予玩家一枚徽章,其中每個玩家的ID。
檢查一個列表的徽章ID與UserId對比,並返回玩家擁有的徽章ID列表。
擷取有ID的徽章的資訊。
檢查玩家是否擁有徽章,並且徽章ID是否與 Player.UserId 相同。
屬性
方法
AwardBadge
授予 Player 一枚徽章,使用 UserId 和徽章 ID。速率限制: 50 + 35 * [使用者數量] 每分鐘 。為了成功授予徽章:
- 玩家必須現在連接到遊戲。
- 玩家不得已擁有徽章 (請注意,玩家可能會刪除已獲得的徽章到他們的個人檔案並再次獲得徽章)。
- 徽章必須在與徽章相關的遊戲的一部分所在的地方授予。
- 徽章必須啟用;使用 IsEnabled 徽章通過 BadgeService:GetBadgeInfoAsync() 獲取的屬性來檢查此點。
也見:
參數
徽章將授予給用戶的 Player.UserId 。
要頒發的徽章ID。
返回
如果徽章獲得成功,則 true 的布林式。
範例程式碼
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
檢查一個列表的徽章ID與UserId對比,並返回玩家擁有的徽章ID列表。
此方法支持最多 10 枚徽章的批量。使用 BadgeService:UserHasBadgeAsync() 單獨查找徽章。
速率限制: 每分鐘 10 + 5*[玩家數量] 在每個伺服器上。
無論誰創建了徽章或徽章用於哪些體驗,都可以查詢任何體驗的徽章集。任何 UserId 都可以在 Script 中使用,但在 LocalScript 中,只有客戶端運行腳本的本地用戶的 UserId 才能使用。
參數
返回
給定使用者擁有的徽章ID列表超出提供的徽章ID。如果提供的徽章中沒有任何一個屬於指定用戶,則為空。不保證與輸入列表相同的順序。
範例程式碼
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
此功能會取得有ID的徽章的資訊。從 Roblox 網站載入資訊需要短暫的時間;重複呼叫會在短時間內緩存。它返回包含以下欄位的辭典:
<th>類型</th><th>說明</th></tr></thead><tbody><tr><td><b>名稱</b></td><td>字串</td><td>徽章的名稱。</td></tr><tr><td><b>說明</b></td><td>字串</td><td>徽章的說明。</td></tr><tr><td><b>圖示圖像ID</b></td><td>int64</td><td>徽章的圖像 ID。</td></tr><tr><td><b>已啟用</b></td><td>bool</td><td>指示徽章是否可以授予。</td></tr></tbody>
關鍵 |
---|
也見:
參數
需要擷取資訊的徽章ID。
返回
一個關於指定徽章的資訊辭典。
範例程式碼
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
檢查並返回是否有 Player 擁有徽章,因為它們的 UserId 和徽章ID。速率限制: 50 + 35 * [玩家數量] 每分鐘 。您可以從服務器中呼叫函數,在 Script 或 ModuleScript 最終由 Script 需要,並且問題的用戶必須存在在服務器上,以便查詢運執行。當從客戶端在 LocalScript 中呼叫方法時,只適用於本地使用者的客戶端運行腳指令碼。
任何遊戲的任何徽章都可以查詢,無論誰創建了徽章或徽章用於哪個體驗。
也見:
參數
玩家的 Player.UserId 檢查指定徽章的擁有權。
檢查所有權的徽章ID。
返回
指示指定使用者是否擁有指定的徽章。
範例程式碼
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)