Global Leaderboard not working

Hello everybody, Im trying to make a Global Leaderboard that updates every 10 seconds (doing this 10 secs for testing) BUT IT DOESN’T WORK!!11
Can anybody help?

INFO: (Both scripts are inside the ServerScriptService, also the first Script works and saves the player data, which is why it is making me ask "why is the global leaderboard script not working? " API is turned on btw!)

Additional info: KP means (Kill points)

Code 1 (Server leaderboard):

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local KPDataStore = DataStore:GetDataStore("KPDataStore")  "KPDataStore"

local function leaderboard(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local kp = Instance.new("IntValue")
	kp.Name = "KP"
	kp.Value = 0 
	kp.Parent = leaderstats


	local success, savedData = pcall(function()
		return KPDataStore:GetAsync(player.UserId)
	end)

	if success and savedData then
		kp.Value = savedData 
	else
		warn("Failed to load data for player:", player.Name)
	end

	kp.Changed:Connect(function()
		local success, err = pcall(function()
			KPDataStore:SetAsync(player.UserId, kp.Value)
		end)
		if not success then
			warn("Failed to save data for player:", player.Name, "-", err)
		end
	end)
end

local function onPlayerRemoving(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	local kp = leaderstats and leaderstats:FindFirstChild("KP")
	if kp then
		local success, err = pcall(function()
			KPDataStore:SetAsync(player.UserId, kp.Value)
		end)
		if not success then
			warn("Failed to save data for player:", player.Name, "-", err)
		end
	end
end

Players.PlayerAdded:Connect(leaderboard)
Players.PlayerRemoving:Connect(onPlayerRemoving)

SCRIPT 2: Global Leaderboard handler

local DataStoreService = game:GetService("DataStoreService")
local KpLb = DataStoreService:GetOrderedDataStore("KpLb")

local function updateleaderboard()
	local success, errorMessage = pcall(function()
		local Data = KpLb:GetSortedAsync(false, 5) 
		local KillPointsPage = Data:GetCurrentPage()

		for rank, data in ipairs(KillPointsPage) do
		
			if data.Key and data.Value then
				local UserName = game.Players:GetNameFromUserIdAsync(tonumber(data.Key))
				local Name = UserName
				local KillPoints = data.Value
				local IsOnLeaderBoard = false

				
				for _, frame in pairs(game.Workspace.GlobalLeaderBoardModel.Screen.LeaderBoardGUI.Holder:GetChildren()) do
					if frame.Player.Text == Name then
						IsOnLeaderBoard = true
						break
					end
				end

				if KillPoints and not IsOnLeaderBoard then
					print("Cloning new leaderboard frame for:", Name)
					local newLBFrame = game.ReplicatedStorage:WaitForChild("PlayerStats"):Clone()
					print("Successfully cloned frame.")
					newLBFrame.Player.Text = Name
					newLBFrame.KP.Text = tostring(KillPoints)
					newLBFrame.Rank.Text = "#" .. rank
					newLBFrame.Position = UDim2.new(0, 0, 0.08 * (#game.Workspace.GlobalLeaderBoardModel.Screen.LeaderBoardGUI.Holder:GetChildren()), 0)
					newLBFrame.Parent = game.Workspace.GlobalLeaderBoardModel.Screen.LeaderBoardGUI.Holder
					print("New Frame Properties - Name:", newLBFrame.Player.Text, "KP:", newLBFrame.KP.Text, "Rank:", newLBFrame.Rank.Text)
				end
			else
				print("Invalid data entry at rank:", rank, "Key:", data.Key, "Value:", data.Value)
			end
		end
	end)

	if not success then
		warn("Error updating leaderboard:", errorMessage)
	end
end

while true do
	for _, Player in pairs(game.Players:GetPlayers()) do
		local success, errorMessage = pcall(function()
			KpLb:SetAsync(Player.UserId, Player.leaderstats.KP.Value)
		end)
		if not success then
			warn("Error saving data for player:", Player.Name, "-", errorMessage)
		end
	end

	for _, frame in pairs(game.Workspace.GlobalLeaderBoardModel.Screen.LeaderBoardGUI.Holder:GetChildren()) do
		frame:Destroy()
	end

	updateleaderboard()
	print("Leaderboard Updated")
	wait(10) 
end

I know it is alot. And I understand if you dont want to help, Any help appreciated though. :pray::heart:

2 Likes

damn bruh but where is the error in the code??

errors?? warnings??

also, use :UpdateAsync to update data

1 Like

Invalid data entry at rank: 1 Key: nil Value: nil - Server - LeaderboardHandler:37

1 Like

i’m not going to count the code lines in the snippet

just send over the code that gives out the error :broken_heart:

The code is printing (Updated leaderboard) but it doesn’t update in the actual Global leaderboard screen in the “Log” it sends the error in the reply i sent you earlier

“just send over the code that gives out the error”

do you mean this:

if not success then
warn(“Error saving data for player:”, Player.Name, “-”, errorMessage)
end
end

(i aint sure)

…right

cough, please learn before making games, no need to fully rely on AI cough because you’ll end up not knowing even the basics cough

i’m asking for the code section that errors, NOT the one that gives a damn unrelated warning in the output

It’s a really small detail, but key and value should both be completely lowercase.

3 Likes

i followed a tutorial looked, Did everything. but couldn’t find the issue I tried to get help from AI because i get nervous when making dev forum posts, Im not trying to argue but everybody is learning and i dont think there is a problem in making a game when u still learning things, That’s why im making a game, To learn. And I know the basics i just never tackled data saving, and leaderboard stuff

because most games i did was single player games.

as mad as i am:

making a game fully out of AI (not assuming you’re doing that), or making ELEMENTS using AI (that you don’t understand) is not the way to learn

i’m not saying i’m pure, i use AI aswell, it’s helpful here and there
but you can’t just rely on it

Yo bro thanks everything is working now :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.