I have this code for my leaderboard but the problem is that my Coins variable is equal to nil, does any one know how to solve this?
script:
local dataStoreService = game:GetService('DataStoreService')
local Coinsleaderboard = dataStoreService:GetOrderedDataStore('CoinsLB')
local function UpdateLeaderboard()
local success, errorMessage = pcall(function()
print('Started')
local data = Coinsleaderboard:GetSortedAsync(false, 5)
local coinPage = data:GetCurrentPage()
if #coinPage == 0 then
print("No data found in the leaderboard.")
return
end
local scrollingFrame = game.Workspace.Leaderboard.Leaderboard.ScrollingFrame
local leaderboardFrames = {}
for _, v in ipairs(scrollingFrame:GetChildren()) do
if v:IsA('Frame') then
table.insert(leaderboardFrames, v)
end
end
for Rank, data in ipairs(coinPage) do
print('Checking stuff')
local username = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local name = username
local coins = data.Value
if coins == nil then
print('Error: Coins value is nil for player ' .. name)
return
end
local frameToUpdate
for _, frame in ipairs(leaderboardFrames) do
if frame.PLAYER.Text == name then
frameToUpdate = frame
break
end
end
if not frameToUpdate then
print('SET NEW')
frameToUpdate = game.ReplicatedStorage.PlayerFrame:Clone()
frameToUpdate.Parent = scrollingFrame
end
frameToUpdate.PLAYER.Text = tostring(name)
frameToUpdate.COINS.Text = tostring(coins)
frameToUpdate.RANK.Text = "#" .. tostring(Rank)
end
end)
if not success then
print(errorMessage)
end
end
while true do
-- Update player data in DataStore
for _, player in pairs(game.Players:GetPlayers()) do
local coinsValue = player.leaderstats.Coins.Value
Coinsleaderboard:SetAsync(player.UserId, coinsValue)
print('Setting coins for player ' .. player.Name .. ' to ' .. coinsValue)
end
-- Update the leaderboard with the latest data
UpdateLeaderboard()
print('Updated!')
wait(10)
end