You need to check the status boolean; not the error string/returned data.
You’ve also nested a 2nd for rank, data in ipairs(MsgOrLevelPage) do loop within the first for rank, data in ipairs(MsgOrLevelPage) do loop. Your code should look more like this.
local Success, MsgOrLevelPage = pcall(function()
local Data = LevelLeaderBoard:GetSortedAsync(false, 5)
return Data:GetCurrentPage()
end)
if not Success then
warn(MsgOrLevelPage)
else
for rank, data in ipairs(LevelPage) do
local UserName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = UserName
local Level = data.Value
local isOnLeaderBoard = false
for i, v in pairs(game.Workspace["LeaderBoard - Levels"].Screen.SurfaceGui.MainFrame.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderBoard = true
break
end
end
if Level and isOnLeaderBoard == false then
local NewLbFrame = game.ReplicatedStorage:WaitForChild("LevelLeaderBoard"):Clone()
NewLbFrame.Name.Text = Name
NewLbFrame.Level.Text = Level
NewLbFrame.Number.Text = rank.."."
NewLbFrame.Position = UDim2.new(0, 0, NewLbFrame.Position.Y.Offset + (30 * #game.Workspace["LeaderBoard - Levels"].Screen.SurfaceGui.MainFrame.Holder:GetChildren()), 0)
NewLbFrame.Parent = game.Workspace["LeaderBoard - Levels"].Screen.SurfaceGui.MainFrame.Holder
end
end
end
So i did try to fix it and i think i did it but if i put a print where the frame get’s cloned it doesn’t even clone so there needs to be something wrong with it:
either Level is nil/false, or isOnLeaderBoard is true. If you’re still deleting the whole leaderboard before generating a new one each time, then isOnLeaderBoard should never be true.
if isOnLeaderBoard does happen to be true, then you break, which exits out of the for loop. You probably want to use continue instead so it moves to the next item.
Instead of deleting the old leaderboard each time, you could update it in that isOnLeaderBoard check
for i, v in pairs(game.Workspace["LeaderBoard - Levels"].Screen.SurfaceGui.MainFrame.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderBoard = true
v.Level.Text = Level
v.Number.Text = rank .. '.'
v.Position = UDim2.new(0, 0, v.Position.Y.Offset + (30 * #game.Workspace["LeaderBoard - Levels"].Screen.SurfaceGui.MainFrame.Holder:GetChildren()), 0)
continue
end
end
GetSortedAsync has actually four parameters. But you can use only two parameters anyway.
local ascending = false
local numberToShow = 50
local min = 1
local max = 100000
local Data = LevelLeaderBoard:GetSortedAsync(ascending, numberToShow, min, max)