BadgeService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
バッジサービス クラスは、バッジ に関連する情報と機能を提供します。バッジはプラットフォーム全体で使用され、プレイヤーの業績と活アクティビティを認識します。プレイヤーにバッジを授与すると、インベントリに追加され、プロフィールページに表示されます。
コードサンプル
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 * [ユーザー数] 分ごと 。バッジを成功裏に授与するには:
- プレイヤーは現在、ゲームに接続している必要があります。
- プレイヤーはすでにバッジを持っていてはならない (プレイヤーが授与されたバッジをプロフィールから削除し、再度バッジが授与されることに注意)。
- バッジは、バッジに関連するゲームの一部である場所で授与する必要があります。
- バッジは有効にする必要があります; バッジが取得された通過のプロパティを使用してこれをチェックします。
参照してください:
パラメータ
バッジが授与されるユーザーの Player.UserId 。
授与されるバッジのID。
戻り値
バッジが成功して授与された場合の 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のリスト。最大長は 10 です。
戻り値
指定されたユーザーが所有するバッジ 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。The badge ID of the badge whose information should be fetched.
戻り値
指定されたバッジに関する情報の辞書。
コードサンプル
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
チェックして、 が持っているバッジを所有しているかどうか、そしてバッジ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)