Leaderboard - Datastore Problem & Question

Hello, this is my first time making a post in creator hub so, sorry if i make something wrong

.

:star: 1. **What do you want to achieve?** Keep it simple and clear!

  • I want to have datastore(s) that save data such as gold, cash, level which saves some of the data like gold and level daily, weekly, and monthly including the normal all time data
  • Then, i want to use some (especially the datas that has timed saves like gold and level to create leaderboards.

:exclamation: 2. **What is the issue?** Include screenshots / videos if possible!

I researched and found out that i need to use OrderedDataStores to make leaderboards, however to this point i saved my data in one single datastore as a table (?) i guess?

⭐ DataStore Code
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerDataX2")
local ReplicatedStorage = game:GetService("ReplicatedStorage")


local defaultPlayerData = {
	

	JoinDate = "",
	
	Cash = 0,
	Level = 1,
	XP = 0,
	Kills = 0,
	Deaths = 0,
	Playtime = 0,
	Joins = 0,
	RobuxSpent = 0,
	ChatMsgs = 0,
	Gems = 0,
	

	YearlyStats = {
		LastStored = nil,
		Cash = 0,
		Playtime = 0,
		Kills = 0,
		Joins = 0,
		RobuxSpent = 0
	},
	MonthlyStats = {
		LastStored = nil,
		Cash = 0,
		Playtime = 0,
		Kills = 0,
		Joins = 0,
		RobuxSpent = 0
	},
	WeeklyStats = {
		LastStored = nil,
		Cash = 0,
		Playtime = 0,
		Kills = 0,
		Joins = 0,
		RobuxSpent = 0
	},
	DailyStats = {
		LastStored = nil,
		Cash = 0,
		Playtime = 0,
		Kills = 0,
		Joins = 0,
		RobuxSpent = 0
	},

	TestStats = {
		LastStored = nil,
		Cash = 0,
		Playtime = 0,
		Kills = 0,
		Joins = 0,
		RobuxSpent = 0
	}
}
⭐ Leaderboard Code
-- Configuration
local MaxLeaderboard = 100 -- max amount of frames

-- Datastore
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("PlayerDataX2")
local smallestFirst = false
local numberToShow = MaxLeaderboard
local minValue = 1
local maxValue = 10e30


local success, pages = pcall(function()
	return dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
end)

if not success then
	warn("Failed to retrieve sorted data: " .. tostring(pages))
else
	local top = pages:GetCurrentPage()

	for rank, data in ipairs(top) do
		local name = data.key
		local points = data.value
		print(name .. " is ranked #" .. rank .. " with " .. points .. " points")

		-- Clone the appropriate frame based on rank
		local frameToClone
		if rank == 1 then
			frameToClone = Frame1:Clone()
		elseif rank == 2 then
			frameToClone = Frame2:Clone()
		elseif rank == 3 then
			frameToClone = Frame3:Clone()
		elseif rank >= 4 and rank <= 10 then
			frameToClone = Frame10:Clone()
		else
			frameToClone = OtherFrame:Clone()
		end

		-- Set the text for the cloned frame
		frameToClone.NameLabel.Text = name 
		frameToClone.PointsLabel.Text = tostring(points) .. " points" 

		-- Position the frame in the leaderboard
		frameToClone.Parent = MainLeaderboard
		frameToClone.Position = UDim2.new(0, 0, (rank - 1) * 0.1, 0)
	end
end

:grey_question: 3. **What solutions have you tried so far?** Did you look for solutions on the Developer Hub?

I have searched for solutions on dev hub, some were about making global leaderboards, some were about making multiple, some were about timed and some were talking about ordereddatastores, i tried to understand them and make changes in my codes but i couldn’t find out how. I’m pretty sure im making something wrong or completely wrong but i didn’t want to go any further ending up with a mess.

I know im not supposed to ask for codes, nor i will, but i would be very glad if you could tell what i am doing wrong and how could i fix it, i sometimes have problems understanding,

:scroll: 4. Summary

  • I want to set up datastores to save player data like gold, cash, and level, with the ability to save SOME STATS daily, weekly, and monthly stats, along with all-time data.

  • I also want to use these stats, especially the timed ones, to create leaderboards.

  • I was able to create some kind of datastore with the datastore script, and it worked fine when i used stuff like playerdata.JoinDate, however, i couldn’t get it in the leaderboard script as pages or smth.

1 Like