Help with DataStore2 Global Leaderboard

So I made a global leaderboard for kills but now my datastore2 is not working well. I don’t know if I’m doing this right.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("Kills") <---

local gui = script.Parent.Parent
local mainFrame = script.Parent
local List = mainFrame:WaitForChild("List")
local template = script:WaitForChild("Template")

local Board = gui.Parent

local DataStore2 = require(game.ServerScriptService.Saver:WaitForChild("DataStore2"))

local refreashRate = 10 -- seconds
local maxPlayers = 100 -- max players on board

function refreashBoard()
	for i, player in ipairs(game.Players:GetPlayers()) do
		local CurrencyStore = DataStore2("Kills", player) <---
		local value = CurrencyStore:Get()
		if value then
			local newValue = math.round(value * 1000)
			DataStore:SetAsync(player.UserId, newValue)
			repeat task.wait(0.1)
				value = CurrencyStore:Get()
			until value ~= nil
	local success, errorMessage = pcall(function()
		local data = DataStore:GetSortedAsync(false, maxPlayers)
		local page = data:GetCurrentPage()
		for rank, saveData in ipairs(page) do
			local user = game.Players:GetNameFromUserIdAsync(tonumber(saveData.key))
			local currency = saveData.value
			if currency then
				local newTemplate = template:Clone()
				newTemplate.Name = user
				newTemplate.Parent = List


while task.wait(refreashRate) do

What exactly is not working right? Are there any errors or does it not save?

Well, I keep getting errors from other scripts using the same DataStore2.

attempt to compare number <= nil

It think it might be from my global leaderboard because it started happening after I made it.

Can you screenshot full error?

Nothing wrong with the script.

Try printing KillsStore:Get().

Prints “0”.

The error occurs only sometimes.

Everything works fine when I disable all the global leaderboard scripts.

In that case, you can put a check to make sure that when you compare KillsStore:Get() and v.Kills KillsStore:Get() is not nil.

for i,v in ipairs(bla bla bla) do
 local killsstoreval = KillsStore:Get()
 if killsstoreval ~= nil then
  if killsstoreval >= v.Kills then
   return v

Edit: You also should put the same check for v.Kills, just in case.

Ok but I want to fix whatever is happening with the global leaderboard.

I am not sure what happens here, you will have to debug the entire leaderboard system. Maybe it is not how the global leaderboard works itself, but how other scripts communicate with it.

1 Like