I made an global leaderboard script. The issue is that it says it’s an unknown user, and I don’t know what’s wrong with it. I tried switching GetNameFromUserIdAsync to GetUserIdfromNameAsync, but it said the same error. I searched the forums, but couldn’t get anything, nor they were related to my issue. Any help is appreciated!
local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetOrderedDataStore("WinsLeaderboard")
local function updateLeaderboard()
local success, errormessage = pcall(function()
local Data = WinsLeaderboard:GetSortedAsync(false, 5)
local CanesPage = Data:GetCurrentPage()
for Rank, data in ipairs(CanesPage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local Canes = data.value
local isOnLeaderboard = false
for i, v in pairs(game.Workspace.Leader.SurfaceGui.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if Canes and isOnLeaderboard == false then
local newFrame = game.ReplicatedStorage:WaitForChild("UserLeader"):Clone()
newFrame.Player.Text = Name
newFrame.Canes.Text = Canes
newFrame.Rank.Text = "#"..Rank
newFrame.Position = UDim2.new(0, 0, newFrame.Position.Y.Scale + (.08 * #game.Workspace.Leader.SurfaceGui.Holder:GetChildren()), 0)
newFrame.Parent = game.Workspace.Leader.SurfaceGui.Holder
end
end
end)
if not success then
warn(errormessage)
end
end
while true do
for _, Player in pairs(game.Players:GetPlayers()) do
WinsLeaderboard:SetAsync(Player.UserId, Player.leaderstats.Canes.Value)
end
for _, frame in pairs(game.Workspace.Leader.SurfaceGui.Holder:GetChildren()) do
frame:Destroy()
end
updateLeaderboard()
print("updated")
wait(10)
end
This error usually occurs when the userid is not valid. If the userid is below 0 then it will error. I recommend wrapping it in a pcall. Here is an example of what I would do
local name = "Failed to load name"
local success, err = pcall(function()
name = game.Players:GetNameFromUserIdAsync(tonumber(data.key)
end
This defaults to the string defined at the top and you might want to change it if you don’t like that
For this you could just avoid going further if the pcall fails but that is up to you to figure out if you want that. Nonetheless here is the code you could use for skipping the user
If I have to point the simple thing out, try printing the key or alternatively find the functionalities that is responsible for updating that data store, describe how each key is written to the data.
It fixed itself somehow, but the error is still going in output, and it shows a person who played my game once on the leaderboards, even though i’m the richest one. The rank also somehow shows the person as #2, when it’s not.