Hello!
I used TheDevKing’s video on making a global leaderboard and it worked on my Credits leaderboard but when I copied and pasted the script and changed some of the values. The frame (newLBFrame) that was supposed to close did not clone at all.
local DataStoreService = game:GetService("DataStoreService")
local LevelsLeaderboard = DataStoreService:GetOrderedDataStore("LevelsLeaderboard")
local LeaderboardGUI = game.Workspace.OminusOfficesMap.Leaderboards.LevelsLeaderboard.Main.Part.LeaderboardGUI
local function AddCommasToNumber(number)
return tostring(number):reverse():gsub("(%d%d%d)", "%1,"):gsub(",$", ""):reverse()
end
local function UpdateLeaderboard()
local success, errormessage = pcall(function()
local Data = LevelsLeaderboard:GetSortedAsync(false, 5)
local LevelsPage = Data:GetCurrentPage()
for rank, data in ipairs(LevelsPage) do
local UserName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = UserName
local Levels = data.value
local isOnLeaderboard = false
for i, v in pairs(LeaderboardGUI.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if Levels and isOnLeaderboard == false then
local newLBFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
newLBFrame.Player.Text = Name
newLBFrame.Credits.Text = Levels -- Format Credits with commas
newLBFrame.RANK.Text = "#"..rank
newLBFrame.Position = UDim2.new(0,0, newLBFrame.Position.Y.Scale + (0.8 * #LeaderboardGUI.Holder:GetChildren()), 0)
newLBFrame.Parent = LeaderboardGUI.Holder
end
end
end)
if not success then
print(errormessage)
end
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
LevelsLeaderboard:SetAsync(player.UserId, player.leaderstats.Levels.Value)
end
for _, frame in pairs(LeaderboardGUI.Holder:GetChildren()) do
frame:Destroy()
end
UpdateLeaderboard()
print("Updated")
wait(60) -- set to 60
end
newLBFrame.Credits.Text is supposed to be like that.
All help is appreciated!
If you print out the value of Data:GetCurrentPage(), does it show values? I suspect that there might just not be data for it to output
I guess on line 15 would work.
It prints out an empty table.
Here is where I put it.
local LevelsPage = Data:GetCurrentPage()
print(Data:GetCurrentPage())
for rank, data in ipairs(LevelsPage) do
OK, it’s as I suspected then. The leaderboard (loading) script is likely working fine - there just isn’t any data for it to display. Can I please see where you are saving the data to the datastore?
It is a bit messy but here it is:
It is playerDataStore2
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("Test")
local playerDataStore2 = DataStoreService:GetDataStore("LevelTest")
local playerDataStore3 = DataStoreService:GetDataStore("XPText")
local playerDataStore4 = DataStoreService:GetDataStore("LevelMultiTest5")
local function savePlayerData(player)
local success, errorMessage = pcall(function()
playerDataStore:SetAsync(player.UserId, player.leaderstats.Credits.Value)
playerDataStore2:SetAsync(player.UserId, player.leaderstats.Levels.Value)
playerDataStore3:SetAsync(player.UserId, player.XP.XPValue.Value)
playerDataStore4:SetAsync(player.UserId, player.XP.LevelMultiplier.Value)
end)
if not success then
warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
end
end
local function loadPlayerData(player)
local success, data = pcall(function()
return playerDataStore:GetAsync(player.UserId)
end)
local success, data2 = pcall(function()
return playerDataStore2:GetAsync(player.UserId)
end)
local success, data3 = pcall(function()
return playerDataStore3:GetAsync(player.UserId)
end)
local success, data4 = pcall(function()
return playerDataStore4:GetAsync(player.UserId)
end)
if success and data and data2 and data3 and data4 then
player.leaderstats.Credits.Value = data
player.leaderstats.Levels.Value = data2
player.XP.XPValue.Value = data3
player.XP.LevelMultiplier.Value = data4
else
player.leaderstats.Credits.Value = 0
player.leaderstats.Levels.Value = 1
player.XP.XPValue.Value = 0
player.XP.LevelMultiplier.Value = 1
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local credits = Instance.new("IntValue")
credits.Name = "Credits"
credits.Parent = leaderstats
local level = Instance.new("IntValue")
level.Name = "Levels"
level.Parent = leaderstats
local XPFolder = Instance.new("Folder")
XPFolder.Parent = player
XPFolder.Name = "XP"
local XPValueNew = Instance.new("IntValue")
XPValueNew.Parent = XPFolder
XPValueNew.Name = "XPValue"
local LevelMultipler = Instance.new("NumberValue")
LevelMultipler.Parent = XPFolder
LevelMultipler.Name = "LevelMultiplier"
loadPlayerData(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)
Unless you have another script handling it, you aren’t ever saving data to DataStoreService:GetOrderedDataStore("LevelsLeaderboard")
Ohh…
My other Credits leaderboard script did work and I didn’t even do that.
Anyways, how would I do that?
Add another datastore to your :SetAsync chain, which would be the ordered datastore
It would save data to the ordered datastore, such that there is something for the leaderboard loading script to load
Take a look at this script. It is basicly the same but instead of using levels it uses credits. And it works fine. Can you compare the both scripts and see if there is anything wrong with my current one (Levels (the one in my OG post))
local DataStoreService = game:GetService("DataStoreService")
local CreditsLeaderboard = DataStoreService:GetOrderedDataStore("CreditsLeaderboard")
local LeaderboardGUI = game.Workspace.OminusOfficesMap.Leaderboards.CreditsLeaderboard.Main.Part.LeaderboardGUI
local function AddCommasToNumber(number)
return tostring(number):reverse():gsub("(%d%d%d)", "%1,"):gsub(",$", ""):reverse()
end
local function UpdateLeaderboard()
local success, errormessage = pcall(function()
local Data = CreditsLeaderboard:GetSortedAsync(false, 5)
local CreditsPage = Data:GetCurrentPage()
for rank, data in ipairs(CreditsPage) do
local UserName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = UserName
local Credits = data.value
local isOnLeaderboard = false
for i, v in pairs(LeaderboardGUI.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if Credits and isOnLeaderboard == false then
local newLBFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
newLBFrame.Player.Text = Name
newLBFrame.Credits.Text = AddCommasToNumber(Credits) -- Format Credits with commas
newLBFrame.RANK.Text = "#"..rank
newLBFrame.Position = UDim2.new(0,0, newLBFrame.Position.Y.Scale + (0.8 * #LeaderboardGUI.Holder:GetChildren()), 0)
newLBFrame.Parent = LeaderboardGUI.Holder
end
end
end)
if not success then
print(errormessage)
end
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
CreditsLeaderboard:SetAsync(player.UserId, player.leaderstats.Credits.Value)
end
for _, frame in pairs(LeaderboardGUI.Holder:GetChildren()) do
frame:Destroy()
end
UpdateLeaderboard()
print("Updated")
wait(60) -- set to 60
end
I am not quite sure what I did but now it works!
local DataStoreService = game:GetService("DataStoreService")
local LevelsLeaderboard = DataStoreService:GetOrderedDataStore("LevelsLeaderboard")
local LeaderboardGUI = game.Workspace.OminusOfficesMap.Leaderboards.LevelsLeaderboard.Main.Part.LeaderboardGUI
local function AddCommasToNumber(number)
return tostring(number):reverse():gsub("(%d%d%d)", "%1,"):gsub(",$", ""):reverse()
end
local function UpdateLeaderboard()
local success, errormessage = pcall(function()
local Data = LevelsLeaderboard:GetSortedAsync(false, 5)
local LevelsPage = Data:GetCurrentPage()
print(Data:GetCurrentPage())
for rank, data in ipairs(LevelsPage) do
local UserName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = UserName
local Levels = data.value
local isOnLeaderboard = false
for i, v in pairs(LeaderboardGUI.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if Levels and isOnLeaderboard == false then
local newLBFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
newLBFrame.Player.Text = Name
newLBFrame.Credits.Text = Levels -- Format Credits with commas
newLBFrame.RANK.Text = "#"..rank
newLBFrame.Position = UDim2.new(0,0, newLBFrame.Position.Y.Scale + (0.8 * #LeaderboardGUI.Holder:GetChildren()), 0)
newLBFrame.Parent = LeaderboardGUI.Holder
end
end
end)
if not success then
print(errormessage)
end
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
LevelsLeaderboard:SetAsync(player.UserId, player.leaderstats.Levels.Value)
end
for _, frame in pairs(LeaderboardGUI.Holder:GetChildren()) do
frame:Destroy()
end
UpdateLeaderboard()
print("Updated")
wait(60) -- set to 60
end