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.

print(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:

local 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.

local badgeService = game:GetService("BadgeService")

local badgesOwnedCount = 0
for index, badgeID in ipairs(badgeIDs) do
    if badgeService:UserHasBadgeAsync(player.UserId, badgeId) then

    end
end

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

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

Answer this question