Badge counter leaderboard

  1. What do you want to achieve? I want to be able to somehow make a badge counter leaderboard with a table so I can easily put badge ids inside of there instead of multiplying scripts.

  2. What is the issue? I’ve tried scripting one but I’ve had no luck

  3. What solutions have you tried so far? I’ve looked at almost 50+ Dev Forum posts and tried scripting my own but everything was that you have to touch a part to get the stats or it was just a badge counter of every game and not a specific game or specific badge ids

1 Like
local BadgeService = game:GetService("BadgeService");
local badgeIds = {} --Put badge ids here
function countBadges(plr)
    local count = 0;
    for _, id in next, badgeIds do
         if BadgeService:UserHasBadgeAsync(plr.UserId, id)
            count += 1;
         end
    end
    return count;
end

BadgeService:UserHasBadgeAsync() returns whether or not a given user owns a badge.

4 Likes

Should I replace count with my leaderstats?

for some reason it wont work and if i check the script it says this part is underlined in red

They forgot a then after BadgeService:UserHasBadgeAsync(plr.UserId, id).

So this is what I have now

local BadgeService = game:GetService("BadgeService");
local badgeIds = {2124871551, 2124871927, 2124872728, 2124874652, 2124875134, 2124875135, 2124875283, 2124883537, 2124883540, 2124883811, 2124883814, 2124883850, 2124883955} --Put badge ids here
function countBadges(plr)
	local Exorics = 0;
	for _, id in next, badgeIds do
		if BadgeService:UserHasBadgeAsync(plr.UserId, id) then 
			Exorics += 1;
	end
end
return Exorics;
end

i own all of these badges but it wont add to my leader stats

Screen Shot 2022-01-04 at 1.44.34 PM

Have you tried using a loop, it seems to me that it’s only checking once making it so the player would have to keep rejoining or whatever, also you should attempt to use the function as well as you are only returning to the local Value that was created.

countBadges(plr)
local plr = game.Players.Exotic_Stuffing
local Exorics = plr.leaderstats.Exorics
--run this on join, and when they earn an in-game badge increase it manually to avoid spamming API requests
Exorics.Value = countBadges(plr)

Although you may want to find a different solution. Making so many requests for each player joining may cause rate limits for high amounts of badges.

theres a error?

Can you show the explorer for players? Maybe you directed it to the wrong place.

Screen Shot 2022-01-04 at 2.40.23 PM

try this

-- start
game.Players.PlayerAdded:Connect(function(plr)
	local BadgeService = game:GetService("BadgeService")
	local badgeIds = {2124871551, 2124871927, 2124872728, 2124874652, 2124875134, 2124875135, 2124875283, 2124883537, 2124883540, 2124883811, 2124883814, 2124883850, 2124883955} --Put badge ids here
		for _, id in next, badgeIds do
		if BadgeService:UserHasBadgeAsync(plr.UserId, id) then  -- checks if user has badge
			plr.leaderstats.Exorics.Value = plr.leaderstats.Exorics.Value +1 -- if so then adds 1
		end
		end
		end)

also I highly recommend to put the script in ServerScriptService

Try :FindFirstChild(), not sure.

???

Screen Shot 2022-01-04 at 3.41.07 PM

Seems strange that leaderstats is not a valid member when it is there,
is there a delay when the leaderstats is added?
if there is a delay with the leaderstats being added then remove the delay and check if it works!

1 Like

My game ran into a big issue where it was dependent on a leaderstats value upon join, but since they are actually slightly delayed when the user joins, it somehow comes out nil because the leaderstats take a split second to load which causes big problems.

WaitForChild is your best bet here.

Hi, this worked nicely but i’m having problems with the delay too… where do you put WaitForChild?

Whenever you’re retrieving the leaderstats folder, instead of indexing regularly

game.Players.leaderstats.StatName.Value

You’d do this instead to prevent issues.

game.Players:WaitForChild("leaderstats").StatName.Value

Obviously it’s better to use variables here, but for the sake of explanation, that’s where you’d put the :WaitForChild().