How can I make a daily leaderboard?

Hello fellow developers!

Now I am developing an online school
(GE International School)
where I give GE Point according to students’ activities.

Now I am making a global leaderboard according to this article and
the normal one showing accumulated GE Points is working.
However, when I add some codes (in the red rectangle) to make a daily leaderboard, it dosen’t work.(no errors are shown).

I think something wrong with how to script, but I don’t know how to change it.
I appreciate if someone help me.

1 Like

I do not remember exactly how but here is an old code that I had made, maybe it will help you

local TeleportService = game:GetService("TeleportService")
local DataStoreService = game:GetService("DataStoreService")

local dataConfig = game.ReplicatedStorage.DataStoreConfig

local WinsLeaderboard = DataStoreService:GetOrderedDataStore(dataConfig.DataStorageName.Value)

local function updateLeaderboard()
	local success, errorMessage = pcall (function()
		local Data = WinsLeaderboard:GetSortedAsync(false, 7)
		local WinsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(WinsPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local totalXPearned = data.value
			local isOnLeaderboard = false
			for i, v in pairs(script.dir.Value:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end

			if totalXPearned and isOnLeaderboard == false then
				local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				newLbFrame.Player.Text = Name
				newLbFrame.Wins.Text = totalXPearned 
				newLbFrame.Rank.Text = Rank
				--newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * #script.dir.Value:GetChildren()), 0)
				newLbFrame.Parent = script.dir.Value
			end
		end
	end)

	if not success then
		print(errorMessage)
	end
end

while true do

	for _, player in pairs(game.Players:GetPlayers()) do
		WinsLeaderboard:SetAsync(player.UserId, player.totalXPearned.Value)
	end

	for _, frame in pairs (script.dir.Value:GetChildren()) do
		frame:Destroy()
	end

	updateLeaderboard()
	print("Updated!")

	wait(10)
end
local teleportData = TeleportService:GetLocalPlayerTeleportData()

if teleportData then
	local Player = game.Players:GetPlayerByUserId(teleportData.PlayerName)
	Player:WaitForChild('totalXPearned').Value = Player.totalXPearned.Value + 1
end
1 Like

Thank you for your help, but I am afraid no part of your code seems to have variables or function regarding date or time (such as os.time, etc.)

the daily leaderboard doesnt work probably because what you did will only reset the key.
to make a daily leaderboard you will want to store the accumulated point NOT the total point.
basically what happen in the current one is :

  1. a day passed
  2. the leaderboard data key changed. which means the datastore is now an empty table
  3. the player joined
  4. after the player joined the empty datastore stores the current point, which makes it work just like the non-daily leaderboard, since the current point is the Total Point.

easy way to fix will be to add another Value named “Accumulated_Point” and reset it daily, use the “Accumulated_Point” to make the daily leaderboard not the Total Point.

WARNING : do not reset the “Accumulated_Point” before inserting the “Accumulated_Point” to leaderboard or else if you do it then the leaderboard wouldn’t work.

Sorry, if my english bad, not a native speaker.

1 Like

Thank you for your reply and sorry for slow res.
I didn’t tell you guys but I use a data module for leaderstats
(copied from a book of Roblox):

Your advice seems good but things are too complicated for me now
and I don’t know how to fix it.
I will try some and give you feedback later.

Btw, today I continued to research and found a nice article
I think this is educational as reference.

P.S. Don’t mind your English. I am not a native speaker too.