Badge for top 100 people on leaderboard

I need help on a script. I tried searching Google and DevForum, but I can’t find anything on the exact topic. I have a leaderboard for the people that are in the top 100 for amount of candy. I want to give them a badge for being on that leaderboard. I can’t find anything for that, I can only find help for giving someone a badge when they have a certain amount on their leaderstats. Is anyone able to help?

7 Likes

I believe this documentation may help:

You will need the players’ userID’s.

2 Likes

I don’t see anything that can help? This is what I have right now. I need help with the “IsOnLeaderboard” part.

local IsOnLeaderboard = --What do I put here?
local BadgeID = 0
local plr = game.Players.LocalPlayer

if IsOnLeaderboard == true then
	local b = game:GetService("BadgeService")
	b:AwardBadge(plr.userId, BadgeID)
end
2 Likes

Is the leaderboard an orderedDatastore? How do you place the players on the leaderboard? Can you send the script?

1 Like

Hello there,
I am not a programmer but what I think is that:

Get service “Is user on database” if true then
badge service award ID
end

As we want to check if the user is in said database, we would need an API to check that information inside Roblox, and then get the badge service for that.

1 Like

Yes, it is an orderedDatastore. Here is the entire script.

local sg = script.Parent
local sample = script:WaitForChild("Sample") --Our Sample frame
local sf = sg:WaitForChild("ScrollingFrame") --The scrolling frame
local ui = sf:WaitForChild("UI") --The UI list layout

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("candyDataStore")
wait(10)
while true do
	for i,plr in pairs(game.Players:GetChildren()) do--Loop through players
		if plr.UserId>0 then--Prevent errors
			local w = plr.leaderstats.Candy.Value--Get point balance
			if w then
				pcall(function()
				--Wrap in a pcall so if Roblox is down, it won't error and break.
					dataStore:UpdateAsync(plr.UserId,function(oldVal)
				        --Set new value
						return tonumber(w)
					end)
				end)
			end
		end
	end    
	local smallestFirst = false--false = 2 before 1, true = 1 before 2
    local numberToShow = 100--Any number between 1-100, how many will be shown
    local minValue = 1--Any numbers lower than this will be excluded
	local maxValue = 999999999999--9B, any numbers higher than this will be excluded
	local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
    --Get data
    local top = pages:GetCurrentPage()--Get the first page
	local data = {}--Store new data
	for a,b in ipairs(top) do--Loop through data
		local userid = b.key--User id
		local points = b.value--Points
		local username = "[Failed To Load]"--If it fails, we let them know
		local s,e = pcall(function()
		 username = game.Players:GetNameFromUserIdAsync(userid)--Get username
		end)
		if not s then--Something went wrong
		   warn("Error getting name for "..userid..". Error: "..e)
		end
		local image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		--Make a image of them
		table.insert(data,{username,points,image})--Put new data in new table
	end
	ui.Parent = script
	sf:ClearAllChildren()--Remove old frames
	ui.Parent = sf
	for number,d in pairs(data) do--Loop through our new data
		local name = d[1]
		local val = d[2]
		local image = d[3]
		local color = Color3.new(1,1,1)--Default color
		if number == 1 then
			color = Color3.new(1,1,0)--1st place color
		elseif number == 2 then
			color = Color3.new(0.9,0.9,0.9)--2nd place color
		elseif number == 3 then
			color = Color3.fromRGB(166, 112, 0)--3rd place color
		end
		local new = sample:Clone()--Make a clone of the sample frame
		new.Name = name--Set name for better recognition and debugging
        new.LayoutOrder = number--UIListLayout uses this to sort in the correct order
		new.Image.Image = image--Set the image
		new.Image.Place.Text = number--Set the place
		new.Image.Place.TextColor3 = color--Set the place color (Gold = 1st)
		new.PName.Text = name--Set the username
		new.Value.Text = val--Set the amount of points
		new.Value.TextColor3 = color--Set the place color (Gold = 1st)
		new.PName.TextColor3 = color--Set the place color (Gold = 1st)
		new.Parent = sf--Parent to scrolling frame
	end
	wait()
	sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)
	--Give enough room for the frames to sit in
	wait(120)
end

local IsOnLeaderboard = --What do I put here?
local BadgeID = 2146785557
local plr = game.Players.LocalPlayer

if IsOnLeaderboard == true then
	local b = game:GetService("BadgeService")
	b:AwardBadge(plr.userId, BadgeID)
else
	print("Not on leaderboard!")
end
1 Like

To award the top one hundred players, you can replace the code in the following documentation with lines of code awarding the top 100 players rather than printing them:

Change the pageSize to 100.

2 Likes

pageSize? (nvm about this, I hadn’t looked at the documentation yet and didn’t know what pageSize was) Also, I want to award the players as well as show on the leaderboard, not only award them. (I don’t know if this is what you meant by “printing them”)

2 Likes

nevermind, i just went through the documentation entirely, sorry for the confusion. also, thanks!

2 Likes

Here is a code example:

local function awardTopHundredPlayers()
	local isAscending = false
	local pageSize = 100
	local pages = dataStore:GetSortedAsync(isAscending, pageSize) -- retrieves leaderboard players
	local topHundred = pages:GetCurrentPage() -- gets top hundred

	-- award all players on the top hundred the associated badge
	for rank, data in ipairs(topHundred ) do
		local userId = data.key
		game:GetService("BadgeService"):AwardBadge(plr.userId, BadgeID)
	end
end
1 Like

Please mark it as a solution if it helped!

I did :slight_smile: Thanks for helping me! Really appreciate it

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.