徽章服務 類提供與徽章相關的資訊和功能。徽章在平台上使用,用於認識玩家的成就和活動。授予玩家徽章後,它將被添加到他們的庫存中,並在他們的個人資料頁面上顯示。
範例程式碼
下面的例子創建了一個 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().
Getting Badge Info
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
參數
返回
範例程式碼
授予徽章
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
參數
返回
範例程式碼
檢查在批量中獲得的徽章
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
參數
預設值:""
返回
範例程式碼
Getting Badge Info
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
參數
返回
範例程式碼
檢查已獲得的徽章
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)