徽章服務 類提供與徽章相關的資訊和功能。徽章在平台上使用,用於認識玩家的成就和活動。授予玩家徽章後,它將被添加到他們的庫存中,並在他們的個人資料頁面上顯示。
範例程式碼
下面的例子創建了一個 awardBadge() 功能,可以處理在授予徽章時可能發生的潛在錯誤。使用通過 BadgeService:GetBadgeInfoAsync() 獲得的徽章的屬性來確認徽章可以授予,並使用 BadgeService:AwardBadge() 來執行此操作。
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 0
local function awardBadge(player, badgeId)
-- 取得徽章資訊
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- 確認徽章可以授予
if badgeInfo.IsEnabled then
-- 獲獎徽章
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awardSuccess then
-- 獎勵徽章功能發生錯誤
warn("Error while awarding badge:", result)
elseif not result then
-- 獎勵徽章功能沒有授予徽章
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)
以下腳本等待任何玩家進入遊戲,並檢查他們是否擁有特定徽章。這有助於創建一個只有玩家擁有特殊徽章才能使用的碰撞過濾或傳送區域。
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 00000000 -- 將其變更為你的徽章 ID
local function onPlayerAdded(player)
-- 檢查玩家是否有徽章
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
-- 如果發生錯誤,發出警告並退出函數
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
-- 根據需求處理玩家的徽章擁有權
print(player.Name, "has badge", badgeId)
end
end
-- 將「玩家新增」事件連接到「onPlayerAdded()」函數
Players.PlayerAdded:Connect(onPlayerAdded)
此腳本檢查用戶在加入體驗時擁有哪些徽章。
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeIds = { 0000000000, 1111111111, 2222222222 } -- 將其變更為你的徽章ID列表
local function onPlayerAdded(player)
-- 檢查玩家是否有任何徽章
local success, result = pcall(function()
return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
end)
-- 如果發生錯誤,發出警告並退出函數
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 的布林式。
範例程式碼
下面的例子創建了一個 awardBadge() 功能,可以處理在授予徽章時可能發生的潛在錯誤。使用通過 BadgeService:GetBadgeInfoAsync() 獲得的徽章的屬性來確認徽章可以授予,並使用 BadgeService:AwardBadge() 來執行此操作。
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 0
local function awardBadge(player, badgeId)
-- 取得徽章資訊
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- 確認徽章可以授予
if badgeInfo.IsEnabled then
-- 獲獎徽章
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awardSuccess then
-- 獎勵徽章功能發生錯誤
warn("Error while awarding badge:", result)
elseif not result then
-- 獎勵徽章功能沒有授予徽章
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。如果提供的徽章中沒有任何一個屬於指定用戶,則為空。不保證與輸入列表相同的順序。
範例程式碼
此腳本檢查用戶在加入體驗時擁有哪些徽章。
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeIds = { 0000000000, 1111111111, 2222222222 } -- 將其變更為你的徽章ID列表
local function onPlayerAdded(player)
-- 檢查玩家是否有任何徽章
local success, result = pcall(function()
return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
end)
-- 如果發生錯誤,發出警告並退出函數
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。
返回
指示指定使用者是否擁有指定的徽章。
範例程式碼
以下腳本等待任何玩家進入遊戲,並檢查他們是否擁有特定徽章。這有助於創建一個只有玩家擁有特殊徽章才能使用的碰撞過濾或傳送區域。
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 00000000 -- 將其變更為你的徽章 ID
local function onPlayerAdded(player)
-- 檢查玩家是否有徽章
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
-- 如果發生錯誤,發出警告並退出函數
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
-- 根據需求處理玩家的徽章擁有權
print(player.Name, "has badge", badgeId)
end
end
-- 將「玩家新增」事件連接到「onPlayerAdded()」函數
Players.PlayerAdded:Connect(onPlayerAdded)