How would I check if a user owns certain badges then reward a badge if they do? I am stuck on only owning a single asset then rewarding the badge.
Hello, @Crimsonisms .
You can check if any given user owns a specific badge using BadgeService:UserHasBadgeAsync()
(the link is to the documentation which provides relevant information and a snippet to get you started on how it works).
Once stated that, you can iterate through each of the required badges and check whether a player owns them all (without exceeding its rate limitations exposed on its documentation entry): If so, then you would run BadgeService:AwardBadge()
to award that badge to the player.
There are multiple approaches to checking various conditions; For this case, and considering the snippet the documentation provides, you may check if the user owns multiple badges by doing something like this:
local BadgeService = game:GetService("BadgeService")
local requiredBadges = {000000, 000000} -- their IDs in that format.
local function checkBadgeOwnership(userId, badgeId)
local success, result = pcall(BadgeService.UserHasBadgeAsync, BadgeService, userId, badgeId)
if success then
return result
else
warn("Error retrieving badge.")
end
end
local function checkIfUserOwnsBadges(userId)
for _, badgeId in ipairs(requiredBadges) do
if not checkBadgeOwnership(userId, badgeId) then return false end
end
-- If reached this point, then means none of the badges were missing, therefore...
return true
end
There’s the small possibility I made a typo since I wrote that code directly into here.
Hope this helps.