Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Leaderboard Error? - [Solved By Reader]

Okay, i've got this error saying; "Players:getNameFromUserId() failed: Unknown user -1"

I have absolutely no idea what this could possibly resemble. Maybe some of you don't feel like reading 82 lines, or some of you do.

If you don't feel like helping, you are free to leave. For those who want to help;

you can look at line 17, and 18. Thats where the error came from (i think)

local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetOrderedDataStore("WinsLeaderboard")

local function updateLeaderboard()
	local newWinners = game.ReplicatedStorage.Winners:Clone()
	
	local model1 = newWinners:WaitForChild("1st")
	local model2 = newWinners:WaitForChild("2nd")
	local model3 = newWinners:WaitForChild("3rd")
	
	newWinners.Parent = workspace
	
    local success, errorMessage = pcall (function()
          local Data = WinsLeaderboard:GetSortedAsync(false, 10)
          local WinsPage = Data:GetCurrentPage()
           for Rank, data in ipairs(WinsPage) do
               local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
               local NameX = userName
               local Cash = data.value
               local isOnLeaderboard = false
               for i, v in pairs(workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
                   if v.Player.Text == NameX then
                       isOnLeaderboard = true
                       break
                    end
                end

                if Cash and isOnLeaderboard == false and data.value > 0 then
                    local newLbFrame = game.ReplicatedStorage.LeaderboardFrame:Clone()
                    newLbFrame.name.Text = NameX
                    newLbFrame.Cash.Text = Cash 
                    newLbFrame.Rank.Text = "#"..Rank
                    newLbFrame.Parent = game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder
				
				if Rank == 1 and data.value > 0 then
					local description = game.Players:GetHumanoidDescriptionFromUserId(tonumber(data.key))
					model1.Humanoid:ApplyDescription(description)
					newLbFrame.BackgroundColor3 = Color3.fromRGB(214, 236, 14)
					
				elseif Rank == 2 and data.value > 0 then
					
					local description = game.Players:GetHumanoidDescriptionFromUserId(tonumber(data.key))
					model2.Humanoid:ApplyDescription(description)
					newLbFrame.BackgroundColor3 = Color3.fromRGB(70, 70, 70)
					
				elseif Rank == 3 and data.value > 0 then
					
					newLbFrame.BackgroundColor3 = Color3.fromRGB(115, 116, 52)
					local description = game.Players:GetHumanoidDescriptionFromUserId(tonumber(data.key))
					model3.Humanoid:ApplyDescription(description)
				end
                end
            end
       end)

    if not success then
        print(errorMessage)
    end
end

wait(2)

while true do
      for _, player in pairs(game.Players:GetPlayers()) do
           WinsLeaderboard:SetAsync(player.UserId, player.leaderstats.Cash.Value)
       end
	
	if game.Workspace:FindFirstChild("Winners") then
		for _, winner in pairs(game.Workspace:FindFirstChild("Winners"):GetChildren()) do
		winner:Destroy()
	end
end

        for _, frame in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
             frame:Destroy()
	    end

        updateLeaderboard()
        print("Updated!")

        wait(10)
end

Thank you for reading :)

[Removed by Leamir, please don't spam lines]

3 comments

0

Almost sure you can't get the name of players that aren't online on that server, you can try using roblox HTTP API to do that

0

Leamir, the title of this post contains the function to do exactly that. The only problem here is that he was trying to give it a negative value, which obviously won't work because no one's userId is a negative value.

0

My guess is that your ordered data store isn't saving right or you might not actually have data in there before you run that function.

1 answer

Players:getNameFromUserId() failed: Unknown user -1 simply meas that the user you are being given ("-1") does not appear...

this thread might be able to help you?

Please take that stupid why are you scrolling thing off your post(s) ...

This discussion is preserved as it was. New answers, comments, and votes are closed.