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

How do I fix Players:GetUserIdFromName() failed because the user does not exist?

I just recently really tried to start scripting and I've run into a problem with a global stats leaderboard. The problem is that I made a local server and tried to use the leaderboard with them and there user Id's are negative numbers (eg: -1 -2 -3) which the script can't use. I've seen some other people with this problem but I'm still unsure of how to fix it. My script is below if you want to look it over. Does anyone have an in-depth fix for this problem?

 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 WinsPage = Data:GetCurrentPage()
                for Rank, data in ipairs(WinsPage) do
			    local userName = game.Players:GetUserIdFromNameAsync(tonumber(data.key))
			           local Name = userName
                       local Wins = 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

                     if Wins and isOnLeaderboard == false then
                            local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
                            newLbFrame.Player.Text = Name
                            newLbFrame.Wins.Text = Wins 
                            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)

      if not success then
            print(errorMessage)
      end
end

while true do

        for _, player in pairs(game.Players:GetPlayers()) do
              WinsLeaderboard:SetAsync(player.UserId, player.leaderstats.Wins.Value)
        end

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

        updateLeaderboard()
        print("Updated!")

        wait(10)
end

2 comments

0

I'm not quite sure if I understand your problem, but if I do then what I would suggest is that you use a "pcall" function.

0

I'm using a pcall. My problem is that it's saved the data of the players from a local server which it can't use because they aren't proper ids and I'm wondering how to get rid of them. Or how to clear the leader board data

2 answers

If I understand correctly you're trying to get the player's name, using its UserId.. However you are using the GetUserIdFromNameAsync() function. That gives you the UserId from the player's name, which is the opposite. For example:

local userID = game.Players:GetUserIdFromNameAsync(playerName)

This would be the correct use of that function. What you need is:

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

4 comments

0

I'll try this now hopefully it works

0

This worked but now I'm getting the error code that I was explaining above which is Players:GetNameFromUserId() failed because HTTP 404 (NotFound)

0

Enable http requests in the game settings

0

If this fixed it mark the answer as accepted

I changed

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

to

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

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