I tried to implementing my own leaderboard and it does not seem to update correctly. Let me explain, the player’s points only updates when the server is born, (new server is created). That means to update the global leaderboard, I have to shutdown all the servers for it to update. This is very impratical. I was wondering how to update it in game. Here is my serverscript:
--game services
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--retrieving global ordered data store
local PointsDataStore = DataStoreService:GetOrderedDataStore("PointsDataStore")
local data = {}
--serverside connection for pressing the "export to points" gui button
ReplicatedStorage.ExportPoints.OnServerEvent:Connect(function(player)
local nutrients = game.Players[player.Name].leaderstats.Nutrients.Value
local kills = game.Players[player.Name].leaderstats.Kills.Value
--adds nutrients earned from current session to data store
local val = nutrients + kills
local value = PointsDataStore:IncrementAsync(player.UserId, val)
game.Players[player.Name].leaderstats.Nutrients.Value = 0
game.Players[player.Name].leaderstats.Kills.Value = 0
game.Players[player.Name].PlayerGui.Sidebar.Frame.points.Text = value
end)
ReplicatedStorage.AddPlayerPoints.Event:Connect(function(playerId, amount)
PointsDataStore:IncrementAsync(playerId, amount)
end)
ReplicatedStorage.SubtractPlayerPointsCallback.OnServerEvent:Connect(function(player, price)
local playerPoints = tonumber(PointsDataStore:GetAsync(player.UserId))
local balance = playerPoints - price
if playerPoints >= price then
PointsDataStore:SetAsync(player.UserId, balance)
ReplicatedStorage.SubtractPlayerPointsCallback:FireClient(player)
end
end)
--when player joins, display stored points
Players.PlayerAdded:Connect(function(player)
local DataStorePages = PointsDataStore:GetSortedAsync(false, 12, 1, 10e30)
local top = DataStorePages:GetCurrentPage()
wait(3) --prevents remote event data leaking
for _, v in ipairs(top) do
local points = v.value
local username = Players:GetNameFromUserIdAsync(v.key)
table.insert(data, {username, points})
end
game.ReplicatedStorage.GlobalLeaderboard:FireAllClients(data)
while wait(0.5) do
local value = PointsDataStore:GetAsync(player.UserId)
game.Players:WaitForChild(player.Name).PlayerGui:WaitForChild("Sidebar").Frame.points.Text = tostring(value)
end
end)
Don’t get me wrong, the points display properly, just that it never updates until a new server is created. Here is my client side code:
game.ReplicatedStorage.GlobalLeaderboard.OnClientEvent:Connect(function(array)
for index, str in pairs(numArray) do
if array[index] == nil then
script.Parent.Leaderboard.Frame.ScrollingFrame:FindFirstChild(str).Text = "No one"
else
script.Parent.Leaderboard.Frame.ScrollingFrame:FindFirstChild(str).Text = array[index][1]
end
if array[index] == nil then
script.Parent.Leaderboard.Frame.ScrollingFrame:FindFirstChild("E" .. str).Text = "0"
else
script.Parent.Leaderboard.Frame.ScrollingFrame:FindFirstChild("E" .. str).Text = array[index][2]
end
end
end)
Much help is greatly appreciated. This is the only thing that is preventing me from finishing this global leaderboard. If it is possible, I want the leaderboard to update every minute.