BadgeService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务

徽章服务 类提供有关徽章的信息和功能。徽章被用户在平台上使用来识别玩家的成就和1) 活动 2)动态(as in friend activity)。 在授予徽章给玩家时,它将被添加到其库存中并显示在其个人页面上。

代码示例

Awarding a Badge

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)
Checking Earned Badges

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)
Checking Earned Badges in Batches

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)
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

概要

方法

属性

方法

AwardBadge

暂停

授予 Player 一个徽章,其中 UserId 和徽章 ID。奖励上限: 50 + 35 * [用户每分钟] 。以便成功奖励徽章:

  • 玩家必须现在连接到游戏。
  • 玩家不能已经有徽章(注意玩家可以从他们的个人资料中删除已获得的徽章,并且再次获得徽章)。
  • 徽章必须从服务器端的 Script 或终极从 ModuleScript 获得,这必须是由 Script 生成的,不是从 2>Class.LocalScript2> 。
  • 徽章必须在与徽章相关的游戏中的某个地方获得。
  • 徽章必须启用;使用 IsEnabled 获取的徽章属性来检查它。

还请参阅:

参数

userId: number

徽章的用户身份为 Player.UserId

badgeId: number

获得勋章的徽章ID。


返回

如果徽章获得成功,那么是 true

代码示例

Awarding a Badge

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 中,只有 2>Class.Player.UserId|userId2> 的本地用户才能使用

参数

userId: number

玩家的 UserId 用于检查玩家是否拥有指定的徽章。

badgeIds: Array

徽章的ID列表用于检查所有者的拥有。最大长度为10。


返回

以下是提供给用户的徽章 ID 列表。如果没有提供给用户的徽章 ID,请空白。 不保证与输入列表相同。

代码示例

Checking Earned Badges in Batches

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>
钥匙类型描述

还请参阅:

参数

badgeId: number

徽章的徽章ID,其信息应该被获取。


返回

一个包含有关指定徽章的信息的字典。

代码示例

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

暂停

检查和返回是否拥有一个 Player 的徽章,其 Class.Player.UserId|UserId</

对于任何游戏的任何徽章,无论是谁创建了徽章或它用于哪个体验,都可以进行查询。

还请参阅:

参数

userId: number

玩家的 Player.UserId 用于检查指定徽章的所有权。

badgeId: number

将检查其所有者的徽章的 ID。


返回

指示指定用户是否拥有指定徽章。

代码示例

Checking Earned Badges

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)

活动