I need help with a global leaderboard. worked fine some weeks ago but now its broken for some reason

I’m currently using this script for my global leaderboard. worked fine a while ago but it broke for some reason. I used to search for the issue and the problem is that the data key is -2 and not the player id. can someone help me fixing this problem?

here is the script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("YumsSave")

local MainFrame = script.Parent.Parent
local RefreshRate = 20

local function RefreshLeaderboard()
	for i, Player in pairs(game.Players:GetChildren()) do
		DataStore:SetAsync(Player.UserId, Player.leaderstats.Yums.Value)
		print("refreshed")
	end
	print("3")
	local success, Error = pcall(function()
		local success2, Error2 = pcall(function()
		local Data = DataStore:GetSortedAsync(false, 12)
			local CreditsPage = Data:GetCurrentPage()
		end)
		print("1")
		if Error2 then
			RefreshLeaderboard()
		end
		local Data = DataStore:GetSortedAsync(false, 12)
		local CreditsPage = Data:GetCurrentPage()
		for Rank, SavedData in ipairs(CreditsPage) do
			print(292828)
			print(SavedData.key)
			local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))
			print(1)
			local Credits = SavedData.value
			print(2)
			if Credits and not script.Parent:FindFirstChild(Username) then
				print(3)
				local NewSample = script.Sample:Clone()
				print("clone")
				NewSample.Visible = true
				print("visible")
				NewSample.Parent = MainFrame.SurfaceGui.ScrollingFrame
				NewSample.Name = Username
				NewSample.PName.Text = Username
				if Credits <= 1000 then
					NewSample.Value.Text = Credits.." Yums"
				else
					if Credits >= 1000 and Credits <= 1000000 then
						local num = math.floor(Credits / 1000)
						local deci = math.floor(Credits - (num * 1000))
						NewSample.Value.Text = (num .."."..math.floor(deci / 100).."K Yums")
					else
						if Credits >= 1000000 and Credits <= 1000000000 then
							local num = math.floor(Credits / 1000000)
							local deci = math.floor(Credits - (num * 1000000))
							NewSample.Value.Text = (num .."."..math.floor(deci / 100000).."M Yums")
							else
							if Credits >= 1000000000 and Credits <= 1000000000000 then
								local num = math.floor(Credits / 1000000000)
								local deci = math.floor(Credits - (num * 1000000000))
								NewSample.Value.Text = (num .."."..math.floor(deci / 100000000).."B Yums")
							else
								if Credits >= 1000000000000 then
									local num = math.floor(Credits / 1000000000000)
									local deci = math.floor(Credits - (num * 1000000000000))
									NewSample.Value.Text = (num .."."..math.floor(deci / 100000000000).."T Yums")
								end
							end
						end
					end
				end
				NewSample.Rank.Text = "#"..Rank
			else
				print("uhm not true?!?!")
			end
		end
	end)
end

local suc, err = pcall(function()
	RefreshLeaderboard() 		
end)
if err then
	print(err)
end

while true do
	for i, Frame in pairs(MainFrame.SurfaceGui.ScrollingFrame:GetChildren()) do
		if Frame.Name ~= "Sample" and Frame:IsA("Frame") then
			Frame:Destroy()
			print("destroyed")
		end
	end
	RefreshLeaderboard()
	wait(RefreshRate)
end

When you test with a local server in studio, it uses fake players with negative ID’s (-1, -2, etc.) that will be saved in datastores (unless you specifically protect against that).

I believe this is the problem line because it is using an invalid UserId for these studio players. You should be able to fix the problem by make sure your UserId (tonumber(SaveData.key)) is greater than 0 and just ignore that entry (or fill in with a dummy value)

does it work checking it like this?

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("YumsSave")

local MainFrame = script.Parent.Parent
local RefreshRate = 20

local function RefreshLeaderboard()
	for i, Player in pairs(game.Players:GetChildren()) do
		DataStore:SetAsync(Player.UserId, Player.leaderstats.Yums.Value)
		print("refreshed")
	end
	print("3")
	local success, Error = pcall(function()
		local success2, Error2 = pcall(function()
			local Data = DataStore:GetSortedAsync(false, 12)
			local CreditsPage = Data:GetCurrentPage()
		end)
		print("1")
		if Error2 then
			RefreshLeaderboard()
		end
		local Data = DataStore:GetSortedAsync(false, 12)
		local CreditsPage = Data:GetCurrentPage()
		for Rank, SavedData in ipairs(CreditsPage) do
			print(SavedData.key)
			if SavedData.key >= 0 then
			print(292828)
			
			local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key))	
				
			print(1)
			local Credits = SavedData.value
			print(2)
			if Credits and not script.Parent:FindFirstChild(Username) then
				print(3)
				local NewSample = script.Sample:Clone()
				print("cloneoof")
				NewSample.Visible = true
				print("visible")
				NewSample.Parent = MainFrame.SurfaceGui.ScrollingFrame
				NewSample.Name = Username
				NewSample.PName.Text = Username
				if Credits <= 1000 then
					NewSample.Value.Text = Credits.." Yums"
				else
					if Credits >= 1000 and Credits <= 1000000 then
						local num = math.floor(Credits / 1000)
						local deci = math.floor(Credits - (num * 1000))
						NewSample.Value.Text = (num .."."..math.floor(deci / 100).."K Yums")
					else
						if Credits >= 1000000 and Credits <= 1000000000 then
							local num = math.floor(Credits / 1000000)
							local deci = math.floor(Credits - (num * 1000000))
							NewSample.Value.Text = (num .."."..math.floor(deci / 100000).."M Yums")
						else
							if Credits >= 1000000000 and Credits <= 1000000000000 then
								local num = math.floor(Credits / 1000000000)
								local deci = math.floor(Credits - (num * 1000000000))
								NewSample.Value.Text = (num .."."..math.floor(deci / 100000000).."B Yums")
							else
								if Credits >= 1000000000000 then
									local num = math.floor(Credits / 1000000000000)
									local deci = math.floor(Credits - (num * 1000000000000))
									NewSample.Value.Text = (num .."."..math.floor(deci / 100000000000).."T Yums")
								end
							end
						end
					end
				end
				NewSample.Rank.Text = "#"..Rank
			else
				print("uhm not true?!?!")
			end
			end
		end
	end)
end

local suc, err = pcall(function()
	RefreshLeaderboard() 		
end)
if err then
	print(err)
end

while true do
	for i, Frame in pairs(MainFrame.SurfaceGui.ScrollingFrame:GetChildren()) do
		if Frame.Name ~= "Sample" and Frame:IsA("Frame") then
			Frame:Destroy()
			print("destroyed")
		end
	end
	RefreshLeaderboard()
	wait(RefreshRate)
end

if SavedData.key >= 0 then

The best way to see if something works is to run it and find out!

okay I’II test it! thanks for help

if tonumber(SavedData.Key) >= 0 then

worked and I fixed the problem! thanks for help! didn’t know about this negative studio ids btw.