Global leaderboard help! (argument 1 missing or nil)

this might seem the same as this topic here but the solution to that topic was a typo.

I’ve followed this tutorial to make a global leaderboard, copied everything that’s important and unchangeable. but an error comes saying: Argument 1 missing or nil :frowning:

here is my leaderboard script code:

local DataStoreService = game:GetService("DataStoreService")
local KillsLeaderboard = DataStoreService:GetOrderedDataStore("Kills")
local Players = game:GetService("Players")
local Leaderboard = workspace.LeaderboardFolder.Leaderboard
local Frame = Leaderboard.LeaderboardPart.LeaderboardGui.Holder
local LeaderboardFrame = game.ReplicatedStorage:WaitForChild("LeaderboardList")

local function UpdateLeaderboard()
	local Success,ErrorMessage = pcall(function()
		local Data = KillsLeaderboard:GetSortedAsync(false, 25)
		local Page = Data:GetCurrentPage()
		for Rank,data in ipairs(Page) do
			local Username = Players:GetNameFromUserIdAsync(tonumber(data.key)) -- this part is the error!
			local Name = Username
			local Kills = data.Value
			local IsOnLeaderboard = false
			for i,v in pairs(Frame:GetChildren()) do
				if v:IsA("Frame") and v.Player.Name == Name then
					IsOnLeaderboard = true
					break
				end
			end
			
			if Kills and IsOnLeaderboard == false then
				local NewFrame = LeaderboardFrame:Clone()
				NewFrame.Parent = Frame
				NewFrame.Kills.Text = Kills
				NewFrame.Rank = "#"..Rank
			end
		end
		
	end)
    if not Success then
		warn(ErrorMessage)
	end
end

while true do
	
	for _,player in pairs(Players:GetPlayers()) do
		KillsLeaderboard:SetAsync(player.UserId, player.leaderstats.Kills.Value)
	end
	for _,frame in pairs(Frame:GetChildren()) do
		if frame:IsA("Frame") then
			frame:Destroy()
		end
	end
	UpdateLeaderboard()
	print("Updated!")
	wait(10)
end

I’ve temporarily removed the pcall() to check the line, it seems to lead to line 13 tonumber(data.key)

like the other problem above, i will also include the actual leaderstat creation:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local KillStore = DataStoreService:GetDataStore("Kills")
local DeathsStore = DataStoreService:GetDataStore("Deaths")

Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder",Player)
	Leaderstats.Name = "leaderstats"
	
	local Kills = Instance.new("IntValue",Leaderstats)
	Kills.Name = "Kills"
	Kills.Parent = Leaderstats
	
	local Deaths = Instance.new("IntValue",Leaderstats)
	Deaths.Name = "Deaths"
	Deaths.Parent = Leaderstats
	
	local KillData
	local Success,Error = pcall(function()
		KillData = KillStore:GetAsync(Player.UserId, Player.leaderstats.Kills.Value)
	end)
	
	if Success then
		Kills.Value = KillData
		print(Player.Name.."'s Kills Successfully Loaded!")
	else
		warn(Player.Name.."'s Kills Failed Loading!")
		warn(Error)
	end
	
	local DeathData
	local Success,Error = pcall(function()
		DeathData = DeathsStore:GetAsync(Player.UserId, Player.leaderstats.Deaths.Value)
	end)

	if Success then
		Deaths.Value = DeathData
		print(Player.Name.."'s Deaths Successfully Loaded!")
	else
		warn(Player.Name.."'s Deaths Failed Loading!")
		warn(Error)
	end

end)

Players.PlayerRemoving:Connect(function(Player)
	local Success,Error = pcall(function()
		KillStore:SetAsync(Player.UserId, Player.leaderstats.Kills.Value)
	end)
	if Success then
		print(Player.Name.."'s Kills Saved Successfully!")
	else
		warn(Player.Name.."'s Kills Failed Saving!")
		warn(Error)
	end
	
	local Success,Error = pcall(function()
		DeathsStore:SetAsync(Player.UserId, Player.leaderstats.Deaths.Value)
	end)
	if Success then
		print(Player.Name.."'s Deaths Saved Successfully!")
	else
		warn(Player.Name.."'s Deaths Failed Saving!")
		warn(Error)
	end
end)

game:BindToClose(function()
	wait(3) -- emergency shutdown/playtesting!
end)

for now ignore the “Deaths” leaderstat, as this doesnt contribute to the leaderboard (unless it causes a huge problem then mention otherwise!).

It seems like whatever is in .key is being nil, it’s either the way you’re saving or something else, what is supposed to be in data.key, trying printing both data and data.key in that function and see what they both print

i just did that!
it seemed to print UserId's Kills

which was my old datastore that has been deleted ages ago???

That’s odd, did you by any chance name your new datastore the same as your old datastore?

yes actually! its probably my old datastore trying to override my new datastore

Maybe try making a datastore with a different name from your old Datastore? That could be the issue

yes, i have changed the store from "Kills" to "KillsData"
and it worked perfectly!

1 Like