徽章服务 类提供与徽章相关的信息和功能。徽章在平台上被使用来识别玩家的成就和1) 活动 2)动态(as in friend activity)。在向玩家授予徽章后,它将添加到他们的库存中并显示在他们的个人资料页面上。
代码示例
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获取信息。
检查玩家是否拥有带有 Player.UserId 和徽章ID的徽章。
属性
方法
AwardBadge
授予 Player 一个徽章,带有 UserId 和徽章 ID。速率限制: 50 + 35 * [用户数量] 每分钟 。为了成功授予徽章:
- 玩家必须现在连接到游戏。
- 玩家不得已经拥有徽章(请注意,玩家可以从他们的个人资料中删除获得的徽章,然后再获得徽章)。
- 徽章必须在与徽章相关的游戏的一部分的地方授予。
- 徽章必须启用;使用 IsEnabled 属性检查徽章通过 BadgeService:GetBadgeInfoAsync() 获取。
还见:
参数
徽章将授予给用户的 Player.UserId 。
要授予的徽章的ID。
返回
Boolean 的 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)