GetUserIdFromNameAsync() is failing, help!

Salve Roblox Developers, I am working on a simple leaderboard and I have a part in my script where I use “GetUserIdFromNameAsync()” but there is an error in the output saying this whenever the leaderboard tries to update, resulting in the Player not being displayed on the leaderboard at all.

Players:GetUserIdFromNameAsync() failed: Unknown user - Server - LeaderboardHandler:33

What’s interesting is that this is only occurring when I started to add other scripts to the game, specifically the ones in the image below.

I’m assuming that there is something being interfered with within the other scripts that I added, because when I copied the leaderboard over to a new file it works perfectly fine, and it successfully displays the player on the leaderboard.

LeaderboardHandler Script

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:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Kills = 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 Kills and isOnLeaderboard == false then
				local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				local Rank = "1"
				newLbFrame.Player.Text = Name
				newLbFrame.Kills.Text = Kills
				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.Kills.Value)
	end
	
	for _, frame in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print("Updated")
	wait(15)
end

leaderstats Script

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Parent = leaderstats
	
	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	Deaths.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(char)
		local humanoid
		repeat
			humanoid = char:FindFirstChild("Humanoid")
			wait()
		until humanoid
		
		humanoid.Died:Connect(function()
			print("Died")
			local tag = humanoid:FindFirstChild("creator")
			if tag then
				local killer = tag.Value
				if killer then
					killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
				end
			end
		end)
		
	end)
	
end)

Any solutions?

Isnt the key something like “id_playerId”?

1 Like

If you are testing in a test server then this is common since the player’s UserId is negative, just ignore any users with a negative UserId:

Leaderboard script:
Sorry for the lack of indentation…

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

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = WinsLeaderboard:GetSortedAsync(false, 100) -- This is now 100 since there is a chance some of the top 5 are test players.
local counted = 0
		local WinsPage = Data:GetCurrentPage()
		for rank, data in ipairs(WinsPage) do
if counted < 5 and tonumber(data.key)>=0 then -- This makes sure we only show the top 5
counted += 1
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Kills = 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 Kills and isOnLeaderboard == false then
				local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				local Rank = "1"
				newLbFrame.Player.Text = Name
				newLbFrame.Kills.Text = Kills
				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)
	
	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.Kills.Value)
	end
	
	for _, frame in pairs(game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print("Updated")
	wait(15)
end
1 Like