How to find how many people own a badge?

Long story short, I need to find how many people own a badge within my game. It would appear on a GUI and update, perhaps every 5 or so minutes. How would I accomplish this? I’m guessing HTMLservice (ugh)?

3 Likes

I don’t exactly know. But contacting the badge won’t work, so maybe a global data store?

1 Like

You’d need to use a ‘DataStore’ instance to record the number of players you’ve awarded a badge to.

local Game = game
local DataStoreService = Game:GetService("DataStoreService")
local BadgeStore = DataStoreService:GetDataStore("BadgeStore")

do
	--Some code where a badge is awarded.
	local Success, Result = pcall(function() return BadgeStore:IncrementAsync(BadgeId, 1) end)
	if not Success then warn(Result) end
end

I see. Would this not get overloaded, updating the same dataStore key constantly across dozens of servers?

Would largely depend on how frequently the badge is awarded, if you expect it to be awarded at least once per six seconds within a single server then yes.

DataStoreService request limits are typically per server, if you don’t mind a slight loss of accuracy you could increase the value periodically or when the server closes via a counter.

local Game = game

local Counter = 0

do
	--Badge awarded.
	Counter += 1
end

local function OnGameClose()
	--Increase DataStore key's value by value of 'Counter'.
end

Game:BindToClose(OnGameClose)
1 Like

In that case this should be totally fine. There are chances that it can happen more frequently that once every six seconds, say someone tags multiple people in a row, but perhaps I can bottleneck it somehow.

1 Like

There’s a much better solution to your problem, you can use Roblox’s own API to check how many people own a badge.

local http = game:GetService("HttpService")

local badgeId = 0
local data = http:GetAsync("https://badges.roproxy.com/v1/badges/"..badgeId)

if data then
	data = http:JSONDecode(data)
	print(data.statistics.awardedCount)
end

This will send an HTTP Get request and return information about any badge, data.statistics.awardedCount will tell you exactly how many people own the badge at that point in time. Keep in mind this doesn’t update live, not sure how often it updates though.

This also uses a Proxy, which you might be familiar with if you know about HTTP protocols, however, these proxies might become unreliable in the future if hosted by a third party, not to worry since there’s this proxy set up tutorial that is amazing and can explain how to set up your own proxy without any JavaScript knowledge.

7 Likes