Hello,
Recently, I have created a leaderboard script in ServerScriptService for my game.
It controls creating a stage, cash and wins leaderboard, which works great using DataStore2 :)
I have had a couple of issues on a part of this leaderboard, the ordered data store part.
Firstly, it does not order the wins in a high to low order. It has sequenced the players in a 'last updated' order.
For example:
What my leaderboard is showing:
https://cdn.discordapp.com/attachments/356675974928007168/518192064706379787/unknown.png
What I'm trying to get it to show:
https://cdn.discordapp.com/attachments/356675974928007168/518192333758398535/example.png
Secondly, I am having issues where the leaderboard doesn't appear at all. But then other times it appears. I think it is because ROBLOX has troubles with data stores ATM. That's why I switched to DataStore2 on standard data stores. If that is the answer to the problem, how do I use my ordered data store on the DataStore2 system?
This is an example on the leaderboard not appearing:
https://cdn.discordapp.com/attachments/356675974928007168/518193965791576092/unknown.png
This is the DataStore2 module and system:
Module: https://www.roblox.com/library/1936396537/DataStore2-Data-Loss-Prevention-and-Caching
Description: https://devforum.roblox.com/t/how-to-use-datastore2-data-store-caching-and-data-loss-prevention/136317
This is my current code for the leaderboard script in ServerScriptStorage:
The parts of the code that is relevant to the ordered data store leaderboard are lines 1-52.
001 | local DataStore 2 = require(game.ServerScriptService:WaitForChild( "MainModule" )) |
002 | local ODS = game:GetService( "DataStoreService" ):GetOrderedDataStore( "WinsFixed" ) |
003 | local Pages = ODS:GetSortedAsync( false , 10 ) |
004 | local Data = Pages:GetCurrentPage() |
005 | local GUI 1 = game.StarterGui.LeaderboardUI |
006 | local GUI 2 = game.StarterGui.LeaderboardUI 1 |
008 | local function Update() |
009 | local succ,err = pcall ( function () |
010 | local Template = GUI 1. Template |
011 | for i,v in pairs (GUI 1. MainFrame:GetChildren()) do |
012 | if v:IsA( "Frame" ) then |
016 | local Template 2 = GUI 2. Template |
017 | for i,v in pairs (GUI 2. MainFrame:GetChildren()) do |
018 | if v:IsA( "Frame" ) then |
022 | for a,k in pairs (Data) do |
023 | local PlayerName = k.key |
024 | local PlayerPoints = k.value |
025 | local NewTemp = Template:Clone() |
026 | NewTemp.Name = PlayerName |
027 | NewTemp.FrameName.Text = PlayerName |
028 | NewTemp.FrameValue.Text = PlayerPoints |
029 | NewTemp.Visible = true |
030 | NewTemp.Parent = GUI 1. MainFrame |
031 | NewTemp:Clone().Parent = GUI 2. MainFrame |
035 | warn( "An error has occurred when trying to update leaderboard: " .. err) |
040 | game.Workspace.ChildAdded:Connect( function (object) |
041 | local player = game.Players:GetPlayerFromCharacter(object) |
042 | if player ~ = nil then |
043 | local ls = player:WaitForChild( "leaderstats" ) |
044 | local sl = game.Workspace:FindFirstChild(ls.Stage.Value) |
045 | object.UpperTorso.CFrame = object.UpperTorso.CFrame + Vector 3. new( 0 , 3 , 0 ) |
047 | object.UpperTorso.CFrame = sl.CFrame + Vector 3. new( 0 , 3 , 0 ) |
054 | game.Players.PlayerAdded:Connect( function (player) |
056 | local cashDataStore = DataStore 2 ( "cash" ,player) |
057 | local deathsDataStore = DataStore 2 ( "deaths" ,player) |
058 | local standardDataStore = DataStore 2 ( "standard" ,player) |
059 | local perfectDataStore = DataStore 2 ( "perfect" ,player) |
060 | local speedDataStore = DataStore 2 ( "speed" ,player) |
062 | local leaderstats = Instance.new( "Folder" ,player) |
063 | leaderstats.Name = "leaderstats" |
065 | local winstats = Instance.new( "Folder" ,player) |
066 | winstats.Name = "winstats" |
068 | local stage = Instance.new( "IntValue" ,leaderstats) |
072 | local cash = Instance.new( "IntValue" ,leaderstats) |
075 | local deaths = Instance.new( "IntValue" ,winstats) |
076 | deaths.Name = "Deaths" |
078 | local BestPerfect = Instance.new( "IntValue" ,winstats) |
079 | BestPerfect.Name = "BestPerfect" |
081 | local BestStandard = Instance.new( "IntValue" ,winstats) |
082 | BestStandard.Name = "BestStandard" |
084 | local BestSpeed = Instance.new( "IntValue" ,winstats) |
085 | BestSpeed.Name = "BestSpeed" |
087 | local specialmode = Instance.new( "BoolValue" ,winstats) |
088 | specialmode.Name = "Special" |
090 | local mode = Instance.new( "StringValue" ,leaderstats) |
092 | mode.Value = "Standard" |
094 | local perfectmode = Instance.new( "BoolValue" ,winstats) |
095 | perfectmode.Name = "Perfect" |
097 | local speedmode = Instance.new( "BoolValue" ,winstats) |
098 | speedmode.Name = "Speed" |
100 | local Wins = Instance.new( "NumberValue" ,winstats) |
103 | local function cashUpdate(updatedValue) |
104 | cash.Value = cashDataStore:Get(updatedValue) |
107 | local function deathsUpdate(updatedValue) |
108 | deaths.Value = deathsDataStore:Get(updatedValue) |
111 | local function BestStandardUpdate(updatedValue) |
112 | BestStandard.Value = standardDataStore:Get(updatedValue) |
115 | local function BestPerfectUpdate(updatedValue) |
116 | BestPerfect.Value = perfectDataStore:Get(updatedValue) |
119 | local function BestSpeedUpdate(updatedValue) |
120 | BestSpeed.Value = speedDataStore:Get(updatedValue) |
124 | cashDataStore:OnUpdate(cashUpdate) |
127 | deathsDataStore:OnUpdate(deathsUpdate) |
129 | BestStandardUpdate( 0 ) |
130 | standardDataStore:OnUpdate(BestStandardUpdate) |
133 | perfectDataStore:OnUpdate(BestPerfectUpdate) |
136 | speedDataStore:OnUpdate(BestSpeedUpdate) |
138 | player.CharacterAdded:Connect( function (Character) |
140 | Character:WaitForChild( "Humanoid" ).Died:Connect( function () |
143 | local deathsDataStore = DataStore 2 ( "deaths" ,player) |
144 | deathsDataStore:Increment( 1 , 0 ) |
151 | if (player.winstats.BestPerfect.Value = = 11 ) and (player.leaderstats.Stage.Value > player.winstats.BestSpeed.Value) then |
152 | if player.winstats.Perfect.Value ~ = true then |
153 | local speedDataStore = DataStore 2 ( "speed" ,player) |
154 | speedDataStore:Set(player.leaderstats.Stage.Value) |
157 | elseif (player.winstats.BestStandard.Value = = 11 ) and (player.leaderstats.Stage.Value > player.winstats.BestPerfect.Value) then |
158 | local perfectDataStore = DataStore 2 ( "perfect" ,player) |
159 | perfectDataStore:Set(player.leaderstats.Stage.Value) |
161 | elseif player.leaderstats.Stage.Value > player.winstats.BestStandard.Value then |
162 | local standardDataStore = DataStore 2 ( "standard" ,player) |
163 | standardDataStore:Set(player.leaderstats.Stage.Value) |
168 | local cashDataStore = DataStore 2 ( "cash" ,player) |
169 | cashDataStore:Increment( 1 , 0 ) |
173 | game.Players.PlayerRemoving:Connect( function (plr) |
174 | local Wins = plr.winstats.Wins |
176 | ODS:IncrementAsync(plr.Name, Wins.Value) |
Any advice you can give will be appricated.