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.