徽章服務 類提供有關 徽章 的信息和功能。徽章在平台上使用,以識別玩家的成就和活動。 當給予玩家徽章時,它們會被添加到他們的道具欄中,並在他們的個人頁面上顯示。
範例程式碼
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)
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)
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)
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 。
檢查一個徽章 ID 清單,並對 UserId 進行檢查,並返回玩家所有有的徽章 ID 清單。
取得標誌的 ID 資訊。
檢查玩家是否擁有 Player.UserId 和徽章 ID。
屬性
方法
AwardBadge
授予 Player 一枚徽章,徽章 ID 是 UserId 和徽章名。獎勵限制: 50 + 35* [用戶每分鐘] 。以成功獎勵的徽章:
- 玩家必須現在連接到遊戲。
- 玩家不能已經擁有徽章 (注意玩家可以從他們的個人資料中刪除已獲得的徽章,並且再次獲得徽章)。
- 徽章必須在徽章與遊戲相關的地方獲得。
- 徽章必須啟用;使用 IsEnabled 來查看徽章獲取方式。
也看:
參數
使用者的 Player.UserId 是獎勵的對象。
獲得徽章的 ID。
返回
如果徽章成功獲得,則 true 是否。
範例程式碼
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 的本地用戶才能使用
參數
返回
提供的徽章 ID 清單中的徽章ID 是給予用戶所有提供的徽章ID 中的一個。 如果沒有提供的徽章都是空的,則不保證與輸入列表相同。
範例程式碼
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 網站載入資訊;重複的呼叫將在一個簡短的時間內儲存。它會返回一個典藏以下字段的字典:
<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>boolean</td><td>指示是否可以獲得徽章。</td></tr></tbody>
鑰匙 | 類型 | 說明 |
---|
也看:
參數
徽章的徽章 ID ,其資訊應該被擷取。
返回
一個包含指定徽章資訊的字典。
範例程式碼
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 擁有給予其 Class.Player.UserId|
任何遊戲的任何徽章都可以查詢,不論是誰創造了徽章或是它的使用者。
也看:
參數
玩家的 Player.UserId 來檢查是否擁有指定的徽章。
檢查其擁有人的徽章 ID。
返回
指示指定用戶是否擁有指定的徽章。
範例程式碼
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)