Specific Badge Counter like The Classic

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hey! So i wanna do a system like in the classic, so you see when you collect a specific badge in a
    game, it a adds up a number to a textlabel like : 1 (1ST BADGE), 2 (SECOND BADGE).

  2. Include screenshots / videos if possible!
    Sorry but i have no screenshots or videos, try to guess.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I did look on youtube and the forum

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
No.

2 Likes

Check this out BadgeService, the thing you are looking for is UserHasBadgeAsync.

Yea i checked it, but i don’t understand, cuz im not that good at scripting…

Well the first thing we need is the BadgeId and the BadgeService

local BadgeId = 00000000 
local BadgeService = game:GetService("BadgeService")

Done, let’s start on the function!

local Check = function(plr)

end
-- or
local function Check()

end

Good, we are able to call it, but it doesn’t do anything. Let’s do a Pcall Function, a protected function that ignores the errors and gives a status back.

local Check = function(Player)
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(Player.UserId, BadgeId)
	end)
	if success then
		
	else
		warn("Player does not own the Bade")
	end
end

Well that was easy, but wait there is still one thing to do. It doesn’t do anything if someone got it, let’s script that real quick!

local Check = function(Player)
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(Player.UserId, BadgeId)
	end)
	if success then
		
	else
		warn("Player does not own the Bade")
	end
	if hasBadge then
		local Val = ... -- Put your Value
		Val += 1
	end
end

Nice! We did it.
Quick Reminder that += is a shortcut of

Val = Val + 1 -- Print 0 + 1
Val += 1 -- Print 0 + 1

FULL CODE

local BadgeId = 00000000 
local BadgeService = game:GetService("BadgeService")

local Check = function(Player)
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(Player.UserId, BadgeId)
	end)
	if success then
		
	else
		warn("Player does not own the Bade")
	end
	if hasBadge then
		local Val = ... -- Put your Value
		Val += 1
	end
end

I hope this helped you!

At the local Val
what do i need to put?

You meant a counter, you may need a value for that. Like a Leaderstat, but please read the whole thing, you need to learn out of it. Also you need to call it by Check(Player)

Yea but as i said in the post description, i need to put it on a textlabel but how?

Well you can make a local script which loops to set the Textlabel as the Value, because what I scripted was a check in the Server and also I kind of messed up. Remove the local BadgeId and put it

local Check = function(Player, BadgeId)

end

Bruh, i don’t understand everyting, can you explain it in a newbie language.

@nayro007
Do you know how to explain it to me but in a newbie language

@nayro007 Please bro explain in a newbie language, i need to know how before the 10th of june

He means do smth like this:
Get the number of badges
Set a textlabel’s text property to the value
And you are done!
Also i do think you should know that you cant ask for someone to make you a script.

--replace with your game badges ids
local gameBadges = {1, 2, 3, 4, 5, 6, 7, 8}

--in case your universe has many badges
--this function needs http requests to work
local function getUniverseBadges(): {number}
	local http = game:GetService("HttpService")
	local badgesAPI = "https://badges.roproxy.com/v1/universes"
	local url = badgesAPI.."/%d/badges?limit=100&sortOrder=Asc"
	local badges = {}
	local cursor = ""
	while cursor do
		--pcall and handle this call, ignoring it for readability
		local response = http:GetAsync(url:format(game.GameId))
		local data = http:JSONDecode(response)
		for _, badge in pairs(data.data) do
			table.insert(badges, badge.id)
		end
		cursor = data.nextPageCursor
	end
	return badges 
end

local function getUserGameBadges(userId: number): {number}
	local BS = game:GetService("BadgeService")
	local badges = {}
	for _, badge in pairs(gameBadges) do
		--also pcall and handle this call
		if BS:UserHasBadgeAsync(userId, badge) then
			table.insert(badges, badge)
		end
	end
	return badges 
end

To count the badges simply use the #(array length) operator on the result of the second function. If you game has many badges do local gameBadges = getUniverseBadges() assuming http requests are on. Combine this with caching and datastores so you don’t have to make a lot of calls many times.

nyrion has given you a script, you shouldn’t even beg someone to provide you with a full script/system, yet you got one.

if you would like something explained to you in an easier-to-understand language, chatgpt 4o at this point would work. if you’re doing a favor for someone on a deadline, you shouldn’t have since you only understand luau in a “newbie language”. if you’re not I don’t really understand the point of a deadline unless you promise your friends or community something, since this is a very tight type of deadline.

i wish you the best of luck but I highly refrain you from doing this again since a bit of people don’t like these posts flooding on the devforum. :slightly_smiling_face: