Hey everyone, once again I’ve been trying to solve this issue for quite a while and I give up and I need to ask for help because even roblox assistant doesn’t work anymore here.
So basically my issue is: at first it worked kinda because it showed every player but the values were outdated, then I changed it so it only shows from cash datastore and after some time it stopped working, now it doesn’t show any errors and doesn’t work.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetOrderedDataStore("cashds_1")
local surfaceGui = script.Parent
local sample = script:WaitForChild("Sample")
local sf = surfaceGui:WaitForChild("ScrollingFrame")
local uiListLayout = sf:FindFirstChildOfClass("UIListLayout") or Instance.new("UIListLayout")
uiListLayout.SortOrder = Enum.SortOrder.LayoutOrder
uiListLayout.Parent = sf
local existingLabels = {} -- Use a dictionary to keep track of existing labels
local function getPlayerCash(userId)
print(userId)
local key = tostring(userId)
local success, cash = pcall(function()
local data = dataStore:GetAsync(key)
print(data)
if data and data.Cash then
return data.Cash
else
return 0
end
end)
if success then
return cash
else
warn("[Global Leaderboard] Error getting cash for player", userId)
return 0
end
end
local function getTopPlayers()
local success, pages = pcall(function()
return dataStore:GetSortedAsync(false, 100, 1, 10e30)
end)
if not success then
warn("[Global Leaderboard] Error getting pages:", pages)
return nil
end
return pages:GetCurrentPage()
end
local function updateLeaderboard()
print("[Global Leaderboard] Updating leaderboard at", os.date("%Y-%m-%d %H:%M:%S"))
local top = getTopPlayers()
if not top then
return
end
-- Clear existing labels
for _, label in pairs(existingLabels) do
label:Destroy()
end
existingLabels = {} -- Reset the dictionary
for rank, entry in ipairs(top) do
local userId = entry.key
print("[Global Leaderboard] Looking up player for userId:", userId)
local player = Players:GetPlayerByUserId(userId)
local color = Color3.new(1, 1, 1)
if rank == 1 then
color = Color3.new(1, 1, 0)
elseif rank == 2 then
color = Color3.new(0.9, 0.9, 0.9)
elseif rank == 3 then
color = Color3.new(166, 112, 0)
end
local new = sample:Clone()
new.Name = userId
new.LayoutOrder = rank
new.Parent = sf
local playerNameLabel = new:WaitForChild("playerName")
local rankLabel = new:WaitForChild("rank")
local valueLabel = new:WaitForChild("value")
if player then
playerNameLabel.Text = player.Name
else
local success, username = pcall(function()
return Players:GetNameFromUserIdAsync(userId)
end)
if success then
playerNameLabel.Text = username
else
warn("[Global Leaderboard] Unable to get username for userId", userId)
playerNameLabel.Text = "Unknown"
end
end
local cash = getPlayerCash(userId)
entry.value = cash
rankLabel.Text = "#" .. rank
rankLabel.TextColor3 = color
valueLabel.Text = cash
valueLabel.TextColor3 = color
playerNameLabel.TextColor3 = color
print("[Global Leaderboard] Player:", player and player.Name or "Not in game", "Cash:", cash)
print("[Global Leaderboard] UI updated for player", userId, "with cash value", cash, "at", os.date("%Y-%m-%d %H:%M:%S"))
existingLabels[userId] = new -- Add the new label to the dictionary
end
end
local function onPlayerChanged(player)
local success, result = pcall(function()
updateLeaderboard()
end)
if not success then
warn("[Global Leaderboard] Error updating leaderboard:", result)
end
local playerGui = player:FindFirstChildOfClass("PlayerGui")
if playerGui then
local coinLeaderboardGui = playerGui:FindFirstChild("coinLeaderboard")
if coinLeaderboardGui then
local surfaceGui = coinLeaderboardGui:FindFirstChild("SurfaceGui")
if surfaceGui then
local sf = surfaceGui:FindFirstChild("ScrollingFrame")
if sf then
local success, result = pcall(function()
updateLeaderboard()
end)
if not success then
warn("[Global Leaderboard] Error updating leaderboard:", result)
end
end
end
end
end
end
Players.PlayerAdded:Connect(onPlayerChanged)
Players.PlayerRemoving:Connect(onPlayerChanged)
-- Function to automatically update leaderboard every 5 seconds
local function autoUpdateLeaderboard()
while true do
local success, result = pcall(function()
updateLeaderboard()
end)
if not success then
warn("[Global Leaderboard] Error updating leaderboard:", result)
end
wait(5) -- Update every 5 seconds
end
end
-- Start the automatic update loop
autoUpdateLeaderboard()
Any help is welcomed because it hurts my head fixing it. I literally spent hours and days with little but no result.