So I use Chatgpt because I suck at datastore.
I try to make 1 leaderboard for 50 datas. How I make it is by use 1 leaderboard with template screen gui and clone it 50 times with leaderboard script inside [so I can switch screen to any data I want].
Here is Clone script
for i, v in pairs(workspace:WaitForChild(“Creatures”):GetChildren()) do
task.wait(3)
local clone = script.Parent.Board:Clone()
clone.Name = v.Name
clone.Parent = script.Parent
task.defer(function()
clone:WaitForChild("Stat").Value = v.Name
end)
end
and leaderboard script that chat gpt made
local LAST_FETCH = 0
local FETCH_COOLDOWN = 35
local GLOBAL_FETCH_LOCK = false
local GLOBAL_SAVE_LOCK = false
local DataStoreService = game:GetService(“DataStoreService”)
local Players = game:GetService(“Players”)
local StatObject = script.Parent.Parent.Parent:WaitForChild(“Stat”)
– wait until Stat is valid
while StatObject.Value == “” do
StatObject:GetPropertyChangedSignal(“Value”):Wait()
end
local function GetStat()
return StatObject.Value
end
local function GetStore()
return DataStoreService:GetOrderedDataStore(“Speedrun_” .. GetStat())
end
script.Parent.Parent.Header.Text = “Top Speedrun Time [” .. GetStat() .. “]”
local SaveCooldown = {}
local UpdateCooldown = false
local LeaderboardCache = {}
– GUI
local function CreateGui()
return script.Parent.PlayerFrame:Clone()
end
– SAFE UPDATE
local function SafeUpdateGui()
if UpdateCooldown then return end
UpdateCooldown = true
UpdateGui()
task.delay(5, function()
UpdateCooldown = false
end)
end
– SAVE
local function SavePlayer(player)
if SaveCooldown[player] then return end
SaveCooldown[player] = true
task.delay(10, function()
SaveCooldown[player] = nil
end)
local statName = GetStat()
local folder = player:FindFirstChild("Speedrun")
if not folder then return end
local statValue = folder:FindFirstChild(statName)
if not statValue then return end
local value = statValue.Value
if value <= 0 then return end
local cs = math.floor(value * 100)
local userId = player.UserId
local improved = false
if GLOBAL_SAVE_LOCK then return end
GLOBAL_SAVE_LOCK = true
task.delay(3, function()
GLOBAL_SAVE_LOCK = false
end)
task.spawn(function()
local success, err = pcall(function()
GetStore():UpdateAsync(userId, function(old)
if not old then
improved = true
return -cs
end
if -cs > old then
improved = true
return -cs
end
return old
end)
end)
if not success then
warn("Save failed:", err)
return
end
if improved then
SafeUpdateGui()
end
end)
end
– FETCH
local function FetchLeaderboard()
LAST_FETCH = LAST_FETCH or 0
local now = os.clock()
local canFetch = (now - LAST_FETCH) >= FETCH_COOLDOWN
if GLOBAL_FETCH_LOCK then
return
end
GLOBAL_FETCH_LOCK = true
task.delay(2, function()
GLOBAL_FETCH_LOCK = false
end)
local success, result = pcall(function()
return GetStore():GetSortedAsync(false, 10)
end)
if not success or not result then
warn("Fetch failed")
LeaderboardCache = {}
return
end
local pages = result:GetCurrentPage()
local newData = {}
for rank, data in ipairs(pages) do
-- 🔥 SAFETY CHECK (IMPORTANT FIX)
if typeof(data.value) ~= "number" then
continue
end
local userId = data.key
local raw = data.value
-- avoid negative confusion
local score = math.abs(raw) / 100
local name = "[Unknown]"
pcall(function()
name = Players:GetNameFromUserIdAsync(userId)
end)
table.insert(newData, {
Name = name,
Score = score,
Rank = rank
})
end
LeaderboardCache = newData
end
– UPDATE GUI
function UpdateGui()
FetchLeaderboard()
-- clear
for _, v in pairs(script.Parent:GetChildren()) do
if v.Name == "Player" then
v:Destroy()
end
end
-- ALWAYS show something (important fix)
if #LeaderboardCache == 0 then
for i = 1, 5 do
local Frame = CreateGui()
Frame.Name = "Player"
Frame.Parent = script.Parent
Frame.Position = UDim2.new(0.05, 0, i * 0.08, 0)
Frame.PlayerName.Text = "No Data"
Frame.Place.Text = "-"
Frame.Score.Text = "0.00"
end
return
end
for i, v in ipairs(LeaderboardCache) do
local Frame = CreateGui()
Frame.Name = "Player"
Frame.Parent = script.Parent
Frame.Position = UDim2.new(0.05, 0, i * 0.08, 0)
Frame.PlayerName.Text = v.Name
Frame.Place.Text = tostring(v.Rank)
Frame.Score.Text = string.format("%.2f", v.Score)
end
end
– PLAYER JOIN
Players.PlayerAdded:Connect(function(player)
local speedrun = player:WaitForChild(“Speedrun”)
local function connectStat()
local statName = GetStat()
local statValue = speedrun:WaitForChild(statName)
statValue.Changed:Connect(function()
task.wait(3)
SavePlayer(player)
end)
end
connectStat()
task.wait(2)
SafeUpdateGui()
end)
– PLAYER LEAVE
Players.PlayerRemoving:Connect(function(player)
SavePlayer(player)
end)
–
IMPORTANT: run once on start
task.defer(SafeUpdateGui)
I tried adding delay so many place but it keeps doom, please help.