So im trying to make a global leaderboard to display the top times on my obby, but it displays your time when you join but won’t update and display your new time if you beat that time.
Here is the code:
local DataStoreService = game:GetService(“DataStoreService”)
local TimeLeaderboard = DataStoreService:GetOrderedDataStore(“TimeLeaderboard”)
local player = game:GetService(“Players”)
local function updateLeaderboard()
local success, errorMessage = pcall(function()
local Data = TimeLeaderboard:GetSortedAsync(false, 5)
local TimePage = Data:GetCurrentPage()
for Rank, data in ipairs(TimePage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local Time = data.value
local isOnLeaderboard = false
for i, v in pairs(game.Workspace.GlobalLeaderboard.GoodPart.LeaderboardGui.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if Time and isOnLeaderboard == false then
local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
newLbFrame.Player.Text = Name
newLbFrame.Time.Text = Time
newLbFrame.Rank.Text = "#"..Rank
newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * #game.Workspace.GlobalLeaderboard.GoodPart.LeaderboardGui.Holder:GetChildren()), 0)
newLbFrame.Parent = game.Workspace.GlobalLeaderboard.GoodPart.LeaderboardGui.Holder
end
end
end)
if not success then
print(errorMessage)
end
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
TimeLeaderboard:SetAsync(player.UserId, player.leaderstats.ObbyTime.Value)
end
for _, frame in pairs(game.Workspace.GlobalLeaderboard.GoodPart.LeaderboardGui.Holder:GetChildren()) do
frame:Destroy()
end
updateLeaderboard()
print("Updated")
wait(10)
end
Thanks in advance!