Why am I getting this error?

So I’m getting an error about my leaderboards and it says

[DataStore name] Players:GetNameFromUserId() failed because HTTP 404 (NotFound)

Why am I getting this error even though I have players on the leaderboards???


Is this because there is a terminated/banned person who was on the leaderboards?

2 Likes

You’re getting the user name from their user IDs, and you’re executing the function on the Players service, meaning that you are getting usernames from the players that are connected on the server. This might be causing an error if your leaderboard is game-wide.

If it is OrderedDataStore, terminated people on the leaderboards causing errors sounds possible. You should cover it in a pcall function to prevent errors and use a backup name like [Failed to load] if it errors. If this is true, then I’d guess it would be because you have to wipe any terminated player’s key (their userId is their key) to prevent it.

Otherwise, it’s just saying you’re trying to get the name of an invalid UserId and that’s all it is.

1 Like

I think it’s game:GetService("Players"):GetNameFromUserIdAsync(UserId)

It is but he would be receiving this error: GetNameFromUserId is not a valid member of Players, yet he did not.

You are correct about me not getting error, @ArtFoundation.

Here is my script btw:

local dss = game:GetService("DataStoreService")
local cashDataStore = dss:GetOrderedDataStore("TotalCash2")
local crystalsDataStore = dss:GetOrderedDataStore("TotalCrystals2")
local cocoaDataStore = dss:GetOrderedDataStore("TotalCocoa2")
local levelDataStore = dss:GetOrderedDataStore("LevelLeaderboard2")
local moltenDataStore = dss:GetOrderedDataStore("MoltenCocoa1")

local formatter = require(game.ReplicatedStorage.Modules.AbbreviationModule)

local ranksNumber = 100
local padding = 0.01

local function setColor(rank, label)
	if rank == 1 then
		label.TextColor3 = Color3.fromRGB(0,255,255)
	elseif rank == 2 then
		label.TextColor3 = Color3.fromRGB(203, 156, 50)
	elseif rank == 3 then
		label.TextColor3 = Color3.fromRGB(126, 130, 148)
	elseif rank > 3 then
		label.TextColor3 = Color3.fromRGB(255,255,255)
	end
end

local function Update(dataStore, holder)
	local success, err = pcall(function()
		local Data = dataStore:GetSortedAsync(false, ranksNumber)
		local page = Data:GetCurrentPage()
		for Rank, data in ipairs(page) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local val = data.value
			local isOnLeaderboard = false
			for i, v in pairs(holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
				
			if val and isOnLeaderboard == false then
				local newFrame = game.ReplicatedStorage:WaitForChild("Template"):Clone()
				newFrame.Player.Text = Name
				newFrame.Rebirths.Text = formatter:format(val)
				newFrame.Rank.Text = "#"..Rank
				setColor(Rank, newFrame.Rank)
				newFrame.Position = UDim2.new(0,0,newFrame.Position.Y.Scale + (0.01 * (#holder:GetChildren())),0)
				newFrame.Parent = holder
			end
		end
	end)
	
	if not success then
		warn(tostring(dataStore)..": "..err)
	end
end

local function SetAsync(dataStore, player, value)
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, value)
	end)
	
	if not success then
		warn(err)
	end
end

local function MainUpdate()
	Update(cashDataStore, workspace.TotalCashLeaderboard.SurfaceGui.Holder)
	Update(crystalsDataStore, workspace.TotalCrystalsLeaderboard.SurfaceGui.Holder)
	Update(cocoaDataStore, workspace.TotalCocoaLeaderboard.SurfaceGui.Holder)
	Update(moltenDataStore, workspace.MoltenLeaderboard.SurfaceGui.Holder)
	Update(levelDataStore, workspace.LevelLeaderboard.SurfaceGui.Holder)
end

while true do
	for _, plr in pairs(game.Players:GetPlayers()) do
		SetAsync(cashDataStore, plr, plr.TotalStats:WaitForChild("TotalCash").Value)
		SetAsync(crystalsDataStore, plr, plr.TotalStats:WaitForChild("TotalCrystals").Value)
		SetAsync(cocoaDataStore, plr, plr.TotalStats:WaitForChild("TotalCocoa").Value)
		SetAsync(moltenDataStore, plr, plr.TotalStats:WaitForChild("TotalMolten").Value)
		SetAsync(levelDataStore, plr, plr:WaitForChild("EXP").Level.Value)
	end
	
	for _, frame in pairs(workspace.TotalCashLeaderboard.SurfaceGui.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	for _, crystalFrame in pairs(workspace.TotalCrystalsLeaderboard.SurfaceGui.Holder:GetChildren()) do
		crystalFrame:Destroy()
	end
	
	for _, cocoaFrame in pairs(workspace.TotalCocoaLeaderboard.SurfaceGui.Holder:GetChildren()) do
		cocoaFrame:Destroy()
	end
	
	for _, levelFrame in pairs(workspace.LevelLeaderboard.SurfaceGui.Holder:GetChildren()) do
		levelFrame:Destroy()
	end

	MainUpdate()
	print("UPDATED")
	
	wait(60)
end

And @ArtFoundation again, I will try your method about the [Username Not Loaded] thing.

EDIT: would this if statement work?

if not userName then
	userName = '[Failed To Load]'
	print("Failed to load username")
end

Yes but did you wrap it in a pcall? Here is my approach:

local success, result = pcall(game.Players.GetNameFromUserIdAsync, game.Players, tonumber(data.key))
result = success and result or "[Failed to load]"
1 Like

Ok it helped me a lot, thank you!