I’m looking into making a Global Leaderboard System for my new game.
I am looking to not only make a global leaderboard system, but one that A) resets every week and B) handles 2 types of stats, Kills and Rebirths.
How would I go about this? Can someone point me in a general direction?
2 Likes
it sets an X timer for the leaderboard and after that time has run out it will reset back to 0
You can change the time of when it resets by just changing “resetInterval” it counts in seconds
local DataStoreService = game:GetService("DataStoreService")
local leaderboardDataStore = DataStoreService:GetDataStore("GlobalLeaderboard")
local resetInterval = 604800 -- time in seconds
local leaderboardData = {
kills = {},
rebirths = {}
}
local function resetLeaderboard()
leaderboardData = {
kills = {},
rebirths = {}
}
leaderboardDataStore:SetAsync("GlobalLeaderboardData", leaderboardData)
end
local function updateLeaderboard()
leaderboardData = leaderboardDataStore:GetAsync("GlobalLeaderboardData") or leaderboardData
-- Update the leaderboard based on the data
end
local function incrementKills(player)
leaderboardData.kills[player.UserId] = (leaderboardData.kills[player.UserId] or 0) + 1
leaderboardDataStore:SetAsync("GlobalLeaderboardData", leaderboardData)
end
local function incrementRebirths(player)
leaderboardData.rebirths[player.UserId] = (leaderboardData.rebirths[player.UserId] or 0) + 1
leaderboardDataStore:SetAsync("GlobalLeaderboardData", leaderboardData)
end
game.Players.PlayerAdded:Connect(function(player)
incrementKills(player)
incrementRebirths(player)
end)
game.Players.PlayerRespawned:Connect(function(player)
incrementRebirths(player)
end)
game:GetService("RunService"):BindToRenderStep("ResetLeaderboard", Enum.RenderPriority.Last.Value, function()
local timeSinceLastReset = os.time() - leaderboardDataStore:GetLastUpdatedTime()
if timeSinceLastReset >= resetInterval then
resetLeaderboard()
end
end)
updateLeaderboard()
2 Likes
Is this truly a global leaderboard that would show for all servers? And in this global leaderboard, would it only reset if the server was up for a full week?
I am asking about a global leaderboard that would cross ALL servers and would reset every week. If need be, I could do that resetting myself.
1 Like
It should, maybe modify somethings around. But it should update and save the data to DataStore and whenever someone joins it it gets the information and displays it again on the leaderboard. whenever the week ends it should just get rid of the data it collected throughout the week
1 Like
I see. What method(s) do I need to use for DataStores to find the top 10 scores for the week at a given time?
You can create a function that saves all stats in the DataStore so you can later get it out.
But just like in that script, you need to get information from “GlobalLeaderboardData” and then let it display on whatever you would like it to
Yes, but what function would I use to pull the data from DataStore, and order the top 10 entries for the week?
I can’t say for certent that it will work. you propably have to modify it around a bit.
But here is en example for how you’d make one for the kills
local function getTop10Kills()
local leaderboardData = leaderboardDataStore:GetAsync("GlobalLeaderboardData") or leaderboardData
local sortedKills = {}
for userId, kills in pairs(leaderboardData.kills) do
table.insert(sortedKills, {userId = userId, kills = kills})
end
table.sort(sortedKills, function(a, b) return a.kills > b.kills end)
local top10 = {}
for i = 1, math.min(#sortedKills, 10) do
local player = game.Players:GetPlayerByUserId(sortedKills[i].userId)
if player then
table.insert(top10, player.Name .. ": " .. sortedKills[i].kills)
end
end
return top10
end