How do I make a badge leaderboard?

So I have a badge hunt game, I want to make a leaderboard and I want to add badges to it, how can I make such a thing? (Check picture for more info)

Picture: lets say the badges you achieved from the game are “stickmen”, how do I make a leaderboard that looks the same?

Screenshot_3

I’m not quite sure what you are after. Could you try providing a picture of what it would look like?

Do you mean something like this:
image

3 Likes

Exactly! I want the leaderboard to count how many badges you have achieved from the game and add them to the leaderboard, how do I make such a thing?

Here’s a guide on how to make a leaderboard: In-Game Leaderboards (roblox.com)

Heres the code for my example:

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder") -- Create the leaderstats
	leaderstats.Name = "leaderstats"
	local badges= Instance.new("NumberValue")
	badges.Name = "Badges" -- Create "Badges" column
	badges.Value = 10 -- Value which they should start at (probably 0)
	badges.Parent = leaderstats
	leaderstats.Parent = plr
end)

To increase the value you must first know when it should increase. Once you know that you only need to execute the following code:

plr.leaderstats.Badges.Value += 1 -- Increases the value by one
1 Like
local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync

local badgeIds = {1, 2, 3} --Replace these numbers with the IDs of the game's badges.

local function onPlayerAdded(player)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = player
	
	local _badges = Instance.new("IntValue")
	_badges.Name = "Badges"
	_badges.Parent = ls
	
	for _, badgeId in ipairs(badgeIds) do
		task.wait(0.1)
		local success, result = pcall(userHasBadge, badges, player.UserId, badgeId)
		if success then
			if result then
				_badges.Value += 1
			end
		else
			warn(result)
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)

Whenever you award badges you’ll need to increment the stat by 1 as well.

8 Likes

Thank you so much! I was wondering how do I make it so that if you earn a badge in-game the leaderboard updates while you’re in-game right away? For instance, I had 8 badges and the leaderboard says 8, I just got a badge and now it should turn 9 right away instead of having to leave and rejoin the server for it to show that I own 9, how do I make that work?

Wherever you have code that awards the player a badge add the following line of code.

player.leaderstats.Badges.Value += 1

player being a reference to that player instance.

You’ll need to make sure the player hasn’t already earned the badge before too.

2 Likes

How would I make the value only go up only once? I was thinking of using boolean values to check, but since it’d be in a serverscript I believe it would stop everybody from being able to retrieve it once the value has been changed? How would I do this locally and efficiently

Use a table that tracks each badge that has been earned per player. Whenever a player collects a badge check to see if that badge exists anywhere in their table before giving it to them

if table.find(tableName, badgeName) ~= nil then
    -- award badge
    table.insert(tableName, badgeName)
end