Show Deprecated
Not Creatable Service
The BadgeService class provides information and functionality related tobadges. Badges are used across theplatform to recognize a player's achievements and activity. Upon awarding abadge to a player, it is added to their inventory and displayed on theirprofile page.
Code Samples
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)
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
Summary
Properties
View all inherited from Instance
Methods
AwardBadge(userId : number,badgeId : number):bool
Yields
Award a badge to a player given the ID of each.
GetBadgeInfoAsync(badgeId : number):Dictionary
Yields
Fetch information about a badge given its ID.
UserHasBadgeAsync(userId : number,badgeId : number):bool
Yields
Checks whether a player has the badge given the Player.UserId andthe badge ID.
View all inherited from Instance
Events
View all inherited from Instance
Properties
View all inherited from Instance
Methods
AwardBadge
bool
Yields
Grants a Player a badge with the UserId andthe badge ID. Rate limit: 50 + 35 * [number of users] per minute. Inorder to successfully award a badge:
The player must be presently connected to the game.
The player must not already have the badge (note that a player maydelete an awarded badge from their profile and be awarded the badgeagain).
The badge must be awarded from a server-side Script or aModuleScript eventually required by a Script, not from aLocalScript.
The badge must be awarded in a place that is part of the game associatedwith the badge.
The badge must be enabled; check this using the IsEnabled property ofthe badge fetched through BadgeService:GetBadgeInfoAsync().
See also:
BadgeService:GetBadgeInfoAsync()
BadgeService:UserHasBadgeAsync()
Parameters
userId: number
The Player.UserId of the user the badge is to be awarded to.
badgeId: number
The ID of the badge to be awarded.
Returns
bool
Boolean of true if the badge was awarded successfully.
Code Samples
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)
GetBadgeInfoAsync
Dictionary
Yields
This function fetches information about a badge given its ID. It takes abrief moment to load the information from the Roblox website; repeatedcalls will cache for a short duration. It returns a dictionary with thefollowing fields:
Key | Type | Description |
---|---|---|
Name | string | The name of the badge. |
Description | string | The description of the badge. |
IconImageId | int64 | The asset ID of the image for the badge. |
IsEnabled | bool | Indicates whether the badge is available to be awarded. |
See also:
BadgeService:AwardBadge()
BadgeService:UserHasBadgeAsync()
Parameters
badgeId: number
The badge ID of the badge whose information should be fetched.
Returns
Dictionary
A dictionary of information about the specified badge.
Code Samples
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
bool
Yields
Checks and returns whether a Player owns a badge given theirUserId and the badge ID. Rate limit: 50 + 35 *[number of players] per minute. You can call the function from theserver in a Script or ModuleScript eventually required bya Script, and the user in question must be present in the serverfor the query to run. When calling the method from the client in aLocalScript, it only works for the local user whose client isrunning the script.
Any badge for any game can be queried, no matter who created the badge orwhich experience it is used for.
See also:
BadgeService:GetBadgeInfoAsync()
BadgeService:AwardBadge()
Parameters
userId: number
The Player.UserId of the player to check for ownership of thespecified badge.
badgeId: number
The badge ID of the badge whose ownership will be checked.
Returns
bool
Indicates if the specified user has the specified badge.
Code Samples
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)
View all inherited from Instance
Events
View all inherited from Instance