LeaderBoard Not Working

I forgot a closing parenthesis

1 Like

So i joined the game and i think the part where i have this:

if MsgOrLevelPage then
warn(MsgOrLevelPage)
else

It is warning in my game and it says this:
image

This is also the script right now what i changed cause you did say it to me to change it:

local DataStoreService = game:GetService("DataStoreService")
local LevelLeaderBoard = DataStoreService:GetOrderedDataStore("DataStoreLevels")

local function Update ()
	local Success, MsgOrLevelPage = pcall(function()
		local Data = LevelLeaderBoard:GetSortedAsync(false, 5)
		return Data:GetCurrentPage()
	end)
		
	for rank, data in ipairs(MsgOrLevelPage) do
			local UserName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = UserName
			local Level = data.Value
			local isOnLeaderBoard = false
			
			if MsgOrLevelPage then
			warn(MsgOrLevelPage)

			else
			for rank, data in ipairs(MsgOrLevelPage) do
					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
		end
	end

Update()

while true do
	
	for _, player in pairs(game.Players:GetPlayers()) do
		LevelLeaderBoard:SetAsync(player.UserId, player.leaderstats.Level.Value)
	end
	
	for _, frame in pairs(game.Workspace["LeaderBoard - Levels"].Screen.SurfaceGui.MainFrame.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	Update()
	print("Updated")
	
	wait(60)
end

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
1 Like

So if i do this it will maybe work but it won’t cause i have this here:
image
and it is cause you deleted at the front so at the local you deleted the ,LevelPage

so do i need to make this MsgOrLevelPage

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:
image

yeah my bad i totally missed that variable

the print prints tho? Are you sure it’s not positioning it off the surfacegui so you can’t see it?

1 Like

So i did add that print and it doesn’t print at all and i looked if the clone was inside of the leaderboard and it isn’t:

image
it also needs to be in the Holder.

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
1 Like

So i did try this and it still doesn’t work but i’m gonna give you the permission to join this game so you can test it: Game Link

So it still doesn’t work.

But the weird thing is thta i don’t get any errors:
image

Hello!

I just looked through your first script and I saw you mistyped a word. I’ll also give advice.

First of all, let’s look at the table you are getting for the leaderboard.

Well, as you can see, you mistyped the word Value. When getting an ordered data store table, it comes with a key and value. Like this

{
    ["key"] = 385026300,
    ["value"] = "Random"
}

Their first letter is not capitalized.


For the advice, let’s glance at the server script that handles loading or saving data.

Here you wrapped something inside pcall. However, I don’t think this is not a good method to know whether the player is a new player or not here.

Imagine it is being failed somehow so you shouldn’t be using it that way. So you couldn’t know if the player was new or not.

1 Like

It worked finally your my hero in this lol but now we have this problem the .Text isn’t a valid string

This is also the error message:
image

i think it is cause i did Name it “Name” and name is also a propertie so it will maybe go to that so i will rename it to PlayerName

Try changing the name of the text label because the script detects it as a property of it.

1 Like

data is saving fine for me ingame; issue has to be with the gui.

1 Like

We fixed it if you didn’t know but now only one player shows up so do you maybe know what’s wrong
cause it does update my stats but not add players:

I suggest that you follow this tutorial:
How to make a simple global leaderboard - Resources / Community Tutorials - DevForum | Roblox

It’s really similar to TheDevKing’s and acts pretty much the same way.

1 Like

Thank you for this information but we did fix it 1 sec ago.

1 Like

Also is there any way to add a limit of how mutch players can be on that screen like max 50 players?

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)
1 Like

So how do you check if 50 players are in it or like how does it work?

And what does it mean cause i don’t really know what GetSortedAsync means cause i never used it before.

It will get the players only whose value take place between the minimum and maximum numbers you gave.

Like if you want a player not to appear on the leaderboard if the value is 0, set the minimum number to 1.

1 Like