Global Leaderboard not working?

Well im getting this error
Argument 1 missing or nil
which leads to

local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))

The key is not the UserId of a player as when you set the Data of the player you have player.UserId … “-time” for GetNameFromUserIdAsync() to work it has to be a straight UserId without* anything else

 local userName = Data.key

also make the D in Data lowercase it will error if not also you previously defined the name of the player on the leaderboard so this line of code is unnecessary

Besides these errors this is my output after fixing them

https://gyazo.com/839e87a0003dfa1af7154b72e1671629

2 Likes

I’ll try it tomorrow. Thank you!

1 Like

So I made some changes, making the key only the UserId. Now, I get no errors, but it still doesn’t work. Here is my new code:

local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetOrderedDataStore("timeDataStore".."-time")

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = WinsLeaderboard:GetSortedAsync(false, 5)
		local WinsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(WinsPage) 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.LeaderboardGUI.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
		
		
			print(userName)
			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.LeaderboardGUI.Holder:GetChildren(), 0)
				newLbFrame.Parent = game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder
			end
			
		
			
		end
		

		
		
		
		
	end)
	
	
end

while true do
	
	for _, player in pairs (game.Players:GetPlayers()) do
		WinsLeaderboard:SetAsync(player.UserId .. "-time", player.leaderstats.TimePlayed.Value)
	end
	
	for _, frame in pairs (game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print("Updated Leaderboard")
	
	

	
	wait(30)
	


	
end
1 Like

Try running it without the pcall. This way, it will show you the error, rather than erring silently (if an error occurs in a pcall, it doesn’t do anything)

What error do you get if you do this?

1 Like

Good idea, trying that quickly.

1 Like

Errors:

Leads to line 9, and line 56.

1 Like

It may have something to do with this line:

Earlier, some one said to add this code:

This adds values to the stores with strings as their keys, not ints. When it runs, it tries to convert the string to an int but can’t because it’s only letter, so it returns nil (I think). Because of this, the get user from userid function will error because the first argument is nil.

If there is nothing else saved to the data store, you could try changing its name and adding this code:

OrderedDataStore:SetAsync("12344", 56)
OrderedDataStore:SetAsync("54321", 68)
OrderedDataStore:SetAsync("21357", 99)

(Those are just random numbers)

1 Like

However, I am saving my data as an int. For reference, here is my data saving script:

local DataStoreService = game:GetService("DataStoreService")
local MarketplaceService = game:GetService("MarketplaceService")

local timeDataStore = DataStoreService:GetDataStore("timeDataStore")


local user

game.Players.PlayerAdded:Connect(function(player)
	
	
		
		
		user = player

		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player

		local timePlayed = Instance.new("IntValue")
		timePlayed.Name = "TimePlayed"
		timePlayed.Parent = leaderstats

		local data

		local success, errormessage = pcall(function(player)
		data = timeDataStore:GetAsync(user.UserId)

		end)

		if success then
			timePlayed.Value = data

		else

			warn("There was a serious error loading data. They have been kicked to prvent dataloss. Error message:".. errormessage)
			user:Kick("There was a critical error loading your data. We had to kick you. Please provide this to the developer if this occurs again: ".. errormessage)

		end
		
		
	
	
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	
	local success, errormessage = pcall(function()
		
		timeDataStore:SetAsync(user.UserId, player.leaderstats.TimePlayed.Value)
		
	end)
	
	if success then
		print("Data has been saved!")
		
	else 
		
		print("There was an error when saving data.")
		print(errormessage)
	end

	
end)




		
	
	
	
local amount = 1
local timename = "TimePlayed"

while true do
	
		
		wait(60)
		for i,v in pairs (game.Players:GetPlayers()) do
			if v:FindFirstChild("leaderstats") then 
				v.leaderstats[timename].Value = v.leaderstats[timename].Value + amount
			end
		end



		local success, errormessage = pcall(function()

		timeDataStore:SetAsync(user.UserId, user.leaderstats.TimePlayed.Value)

		end)

		if success then
			print("Data has been saved!")

		else 

			print("There was an error when saving data.")
			wait(errormessage)
		end
		
		
	end
2 Likes

Did you run this code:
OrderedDataStore:SetAsync(“Jonny”, 56)
OrderedDataStore:SetAsync(“Mary”, 68)
OrderedDataStore:SetAsync(“John”, 99)

If you did, it may have added string keys to your data store, so it wouldn’t be 100% ints

1 Like

Where should I run it? Remember that I’m only using OrderDataStore for getting the leaderboard, the rest of my data is used with an normal datastore.

This is slightly confusing me :confused:

Could you please paste line 9 (this is where the error is, and it’s hard to count lines in the DevForum)

2 Likes
	local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
1 Like

I found this in the DevHub:

The tonumber function must be returning nil, because it’s not a number.

Try replacing line 9 with this:

if tonumber(data.key) == nil then
  local userName = data.key
else
  local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
end

The indentation may be off because I’m on mobile :smile:

1 Like

Sorry i’m late I didn’t get the notification
So if you did run my code to test you should try Removing it and see what happens

OrderedDataStore:RemoveAsync("Jonny")
OrderedDataStore:RemoveAsync("Mary")
OrderedDataStore:RemoveAsync("John")

Also the error is that Argument 1 is missing or nil meaning that

tonumber(data.key)

Is not returning a value
You forgot to capitalize the “data” in the GetNameFromUserIdAsync()

local userName = game.Players:GetNameFromUserIdAsync(tonumber(Data.key)) 

Tell me if this works!

Edit: You should just call RemoveAsync on the lines of code i sent you anyways just in case paste this into the command bar

local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetOrderedDataStore("timeDataStore".."-time")
WinsLeaderboard:RemoveAsync("Jonny")
WinsLeaderboard:RemoveAsync("Mary")
WinsLeaderboard:RemoveAsync("John")

or run this code at the start of the script then play test your game and delete it

WinsLeaderboard:RemoveAsync("Jonny")
WinsLeaderboard:RemoveAsync("Mary")
WinsLeaderboard:RemoveAsync("John")

Still didn’t work… Really weird, no idea. It’s still erroring at line nine and line 56. Really strange.

Same Error im guessing?

Well are you sure theres data in the Data Store? i know you told me there is but lets be sure
put this right before the GetNameFromUserIdAsync()

print(Data.key)

Yes there is data. I’ll check that to test, good idea.

Well, this is super odd. It printed 82738847-time, however it’s only being save to 82738847. I even wiped the data from that. This makes no sense.

The reason GetNameFromUserIdAsync() is returning nil is because you have “-time” added on the end of it thats not a number try removing it and saving some data then run your script

Edit: Ive said that before i dont know if you made the change

Where is the “-time”? It’s not in my script… that’s what is weird.