Med Written By caiomgt

BadgeService Created on: 20-07-2020

Talks about BadgeService and how to award badges!

This tutorial requires you to have understanding on scripting in general.

Welcome, today we are going to talk about badge service, a service which, from the name, you can guess is about badges.

About BadgeService:

BadgeService is a service which has all to do with badges, with 3 functions to help you with handling badges in general, before you continue, you have to own the badge for you to be able to award it, meaning that you will have to buy a badge for 100 robux for this to work(if in a group game, you need the 100 robux in group funds).

Before we continue:

You are going to need your badge's id to use the following functions im about to talk about, BadgeService's functions have web calls, so they may error, meaning you should probably use pcalls for these(if you dont know what pcalls are, there's a great tutorial on lua learning for it), if you dont know how to get your badge's id, it is the random numbers in the badge link after "badges/", without further ado, lets get right into it!

BadgeService:AwardBadge(userId, badgeId)

The function name is self-explanatory, its a function for awarding a badge to the player. You specify first the player's user id for the function to know what player to give the badge to, followed by the badge id so the function knows what badge to award to said player(this function will not error if the player already has said badge and it will also return a boolean reprensenting if the badge has been awarded succesfully or not). A few criteria have to be met first before the player can get the badge awarded succesfully:

  • The player must be connected to the current server for the badge to be awarded successfully The badge must be awarded inside the server, meaning only Scripts or ModuleScripts required by Scripts can award badges The badge must be awarded in a place that is part of the game associated with the badge The badge must be enabled to be awarded.

Example:

BadgeService:AwardBadge(Player.UserId, myBadgeId)

BadgeService:UserHasBadgeAsync(userId, badgeId)

Name is also self explanatory, returns a bool representing whether or not the player with the specified userId has the badge with the id of badgeId. The badge does not need to be owned by the place's owner or the place, it can be used for any badge inside any game. This function also needs to be ran server sided. Example:

--This code would check whether the player has a badge, and if they do, print something
if BadgeService:UserHasBadgeAsync(Player.UserId, aBadgeId) then
	print("User has the badge with id " .. aBadgeID)

BadgeService:GetBadgeInfoAsync(badgeId)

Also has a self-explanatory name, gets info about the badge with the id of badgeId(the badge does not need to be owned by place's owner or the place). It returns a dictionary with the following:

  • Name: It is a string and is the name of the badge. Description: Is a string, which contains the description of said badge. IconImageId: Is an integer, describes the badge's icon id. IsEnabled: It is a bool, which tells you if the badge is enabled or not to be awarded.

Example:

local badgeInfo = BadgeService:GetBadgeInfoAsync(aBadgeId)
print("The badge has the name of " .. badgeInfo.Name .. "with the following description: " .. badgeInfo.Description .. ", it also has the icon id of " .. tostring(badgeInfo.IconImageId) .. ". Badge is available to award:" .. tostring(badgeInfo.IsEnabled))

Thanks for reading my tutorial, hopefully you learned something from it.

See ya soon!

Med Written By caiomgt

See caiomgt's profile on Roblox

Discussion