Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a script that count badges?

Asked by 4 years ago

So I want to make a script that counts badges but I don't know-how. For example.

1print(the number of badges of a player)

1 answer

Log in to vote
3
Answered by 4 years ago

Use the BadgeService: https://developer.roblox.com/en-us/api-reference/class/BadgeService

You could create a table of badges that your game owns:

1local badgeIDs = {}

Then iterate through the table with pairs/ipairs and use :UserHasBadgeAsync (link: https://developer.roblox.com/en-us/api-reference/function/BadgeService/UserHasBadgeAsync) then for each badge the player owns, count up by 1.

1local badgeService = game:GetService("BadgeService")
2 
3local badgesOwnedCount = 0
4for index, badgeID in ipairs(badgeIDs) do
5    if badgeService:UserHasBadgeAsync(player.UserId, badgeId) then
6 
7    end
8end

Then where you see the if statement, under it, you just add one to the badgesOwnedCount for each badge the player owns like so:

1badgesOwnedCount += 1
2-- OR:
3badgesOwnedCount = badgesOwnedCount + 1
0
alright I'll check it tomorrow but thanks! Eric_pokemon 133 — 4y
Ad

Answer this question