How do I get the number one player on leaderboard

So the other day I decided to make a leaderboard for the amount of time you played and it works :slightly_smiling_face:.
But now I am wondering if there is a way can get the first 3 people to give them titles.
There is my script down there. (The ReplicatedStorage was just the way of getting the titles but I have not put the titles yet.)
Thanks for helping!

Server Script

local DataStoreService = game:GetService("DataStoreService")
local TimeplayedLeaderboard = DataStoreService:GetOrderedDataStore("TimeplayedLeaderboard")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = TimeplayedLeaderboard:GetSortedAsync(false, 5)
		local TimeplayedPage = Data:GetCurrentPage()
		for Rank, data in ipairs(TimeplayedPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local TimePlayed = data.value
			local isOnLeaderboard = false
			for i, v in pairs(game.Workspace.TimePlayedLeaderboard.LeaderboardGui.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
			
			if TimePlayed > 0 and isOnLeaderboard == false then
				if Rank <= 10 then
					local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
					newLbFrame.Player.Text = Name
					newLbFrame.TimePlayed.Text = TimePlayed.. "m"
					newLbFrame.Rank.Text = "#"..Rank
					newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.085 * #game.Workspace.TimePlayedLeaderboard.LeaderboardGui.Holder:GetChildren()), 0)
					newLbFrame.Parent = game.Workspace.TimePlayedLeaderboard.LeaderboardGui.Holder
				end
			end
		end
	end)
	
	if not success then
		print(errorMessage)
	end
end

while true do
	
	for _, player in pairs(game.Players:GetPlayers()) do
		TimeplayedLeaderboard:SetAsync(player.UserId, player.leaderstats.Minutes.Value)
	end
	
	for _, frame in pairs(game.Workspace.TimePlayedLeaderboard.LeaderboardGui.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	
	wait(60)
end
2 Likes

yes you get the first 3 values in the ordered datastore

4 Likes

Yeah, but how do I directly call them.

1 Like

Think you can do TimeplayedLeaderboard[1]?

2 Likes

But how do I get the player. Do I do something like TimeplayedLeaderboard[1].UserId?
I’m clueless. :expressionless:

1 Like

You can simply just call TimeplayedLeaderboard[1] if ur data store saves the players, if u want the userid then u have to save the userid directly to that datastore

3 Likes

image
This is what I get

1 Like

Can you show the line of your code?

1 Like

print(TimeplayedLeaderboard[1].UserId)

1 Like

Are you sure you have something saved to that datastore?

3 Likes

What do you mean by that? I dont get the question.

2 Likes

I believe I remember this from a video.

If you want to give the three highest ranked players (1st, 2nd & 3rd) a special rank, you should check that rank.

Something alike underneath (or inside) your if Rank <= 10 then (which I assume only displays the 10 best players

if Rank == 1 then
-- Give them the title. You may want to store a 'BillboardGui' in ServerStorage and :Clone() that to their head, for example.
elseif Rank == 2 then -- And so forth

Keep in mind this title may need to be removed when they get overtaken on the leaderboard, though.

3 Likes

Does the datastore (TimeplayedLeaderboard) have anything on it? what the error say is that there’s nothing inside it…

3 Likes

Yes there is something Inside.

2 Likes

I have tried this but adter cloning do I do something like Tag.Parent = Player.Head?

2 Likes

It’s all up to you what the tag ends up looking like. To clone it, you’d do something like:

function giveSpecialTag(tagMessage, tagColour) -- inside the parenthesis are 'parameters' (the received argumens in order)
	local tag = game.ServerStorage.Tag
	local givenTag = tag:Clone()
	givenTag .Name = "RankTag"
	givenTag .Parent = player.Character.Head

	givenTag .TextLabel.Text = tagMessage
	givenTag .TextLabel.TextColor3 = tagColour
end

And add this under the if Rank == 1 then and customise it thrice depending on your content
giveSpecialTag("SPECIAL TEXT", Color3.fromRGB(250, 250, 50) ) – These are arguments you pass

  • NOTE: You may want to refer to the stored tag beforehand
4 Likes

Where do I get the character? From my knowledge you can only do the Player.Character in local.

2 Likes

There might be an easier way, but this’d probably work as well.

	local player = game.Players:GetPlayerByUserId(tonumber(data.key))
	local character = player.Character or game.Workspace[player.Character] -- Not 100% sure if you need to use both
3 Likes

both of them and one of them give the same result image

2 Likes

Bro i have the same issue rn like what do i do ;-0. And also when u ask people the question they always give random answers like im trying to get the first plr id like how do i do it.