GetNameFromUserIdAsync() issue

So, in the morning this not finished data store script has been working, however now, I encounter this error every time

I don’t understand where the issue is, the code is identical to how it was before, maybe roblox is trying to get user id from name, rather than name from user id, may someone help me?

while true do
	task.wait(7)

	local success, pages = pcall(function()
		return data:GetSortedAsync(false, 10)
	end)

	if success then
		local entries = pages:GetCurrentPage()

		for rank, entry in pairs(entries) do
			local cloneditem = item:Clone()
			local username = game.Players:GetNameFromUserIdAsync(entry.key)

			if playerContainer:FindFirstChild(username) then
				playerContainer:FindFirstChild(username):Destroy()
			end
			if username then
				cloneditem.Name = username
				cloneditem.NameText.Text = username
			end
			cloneditem.RankText.Text = rank
			cloneditem.CashText.Text = entry.value

			cloneditem.Parent = playerContainer
			print("yeah")
		end
	end
end

Roblox is pointing to this line in error
local username = game.Players:GetNameFromUserIdAsync(entry.key)

Try printing the value of key and also show me the key used for setting the datastore.

Also try:

local username = game.Players:GetNameFromUserIdAsync(entry.key:match("%d+"))
1 Like

Oh I see, it prints out this number, rather than user ID
Why is my entry key is not a userid?


When using the match function, it doesn’t find anything in the entries with such user id

Changed: Also, here’s the entries output using print
image

For that, I need to see your entire script.

-2 is the test player if you do server test

Oh, what? That’s something new to see.

Yeah leave it. foodeggs7 made a good point. A fix for this would be just to ignore the data using a pcall.

1 Like

Sorry I did copied the wrong disabled script before, here’s the new one

local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local data = DataStoreService:GetOrderedDataStore("data")
--local data = DataStoreService:GetDataStore("data")
local sessiondata = {}

local item = workspace.Leaderboard:WaitForChild("Item")
local playerContainer = workspace.Leaderboard.Leaderboard.LeaderboardGui.LeaderboardContainer.PlayersContainer

function playerAdded(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local dollars = Instance.new("IntValue")
	dollars.Name = "Kills"
	dollars.Parent = leaderstats
	
	repeat
		local success, PlayerData = pcall(function() 
			return data:GetAsync(plr.UserId, sessiondata[plr.UserId])
		end)
		
		local attempt = 1
		attempt += 1
		if not success then
			warn(PlayerData)
			task.wait(3)
		end
	until
	success or attempt == 5
	
	if success then
		print("yay connected")
		if not PlayerData then
			print("Assigning default data")
			PlayerData = {
				["Kills"] = 0
			}
		end
		sessiondata[plr.UserId] = PlayerData
	else
		warn("That's bad I guess")
		plr:Kick("Unable to load your data. Try again later!!!!!!!!!")
	end
	
	dollars.Value = sessiondata[plr.UserId].Kills

	dollars.Changed:Connect(function()
		sessiondata[plr.UserId].Kills = dollars.Value
	end)
	
	leaderstats.Parent = plr
end

Players.PlayerAdded:Connect(playerAdded)

function playerRemoving(plr)
	if sessiondata[plr.UserId] then
		local success = nil
		local Msg = nil
		local attempt = 1
		
		repeat
			success, Msg = pcall(function() 
				data:SetAsync(plr.UserId, sessiondata[plr.UserId].Kills)
			end)
			
			attempt += 1
			if not success then
				warn(Msg)
				task.wait(3)
			end
		until
		success or attempt == 5
		
		if success then
			print("yay saving data!")
		else
			warn("Unable to save")
		end
		
	end
end

Players.PlayerRemoving:Connect(playerRemoving)

function serverShut()
	if RunService:IsStudio() then
		return
	end
	print("Sever has been shut down")
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			playerRemoving(player)
		end)
	end
end

game:BindToClose(serverShut)

while true do
	task.wait(5)

	local success, errorMsg = pcall(function()
		return data:GetSortedAsync(false, 10)
	end)
	
	if success then
		local pages = data:GetSortedAsync(false, 10)
		local entries = pages:GetCurrentPage()

		for rank, entry in pairs(entries) do
			local cloneditem = item:Clone()
			local username = game.Players:GetNameFromUserIdAsync(entry.key)

			if playerContainer:FindFirstChild(username) then
				playerContainer:FindFirstChild(username):Destroy()
			end
			if username then
				cloneditem.Name = username
				cloneditem.NameText.Text = username
			end
			cloneditem.RankText.Text = rank
			cloneditem.CashText.Text = entry.value

			cloneditem.Parent = playerContainer
			print("yeah")
		end
	end
end

It’s used when you test with multiple players on the server like playtest but server it clone your character and gives an user id with -1 -2 etc also names player1 player2 etc but getNameFromUserIdAsync() things doesn’t work here cuz it’s not an actual player in the database

It’s actually used when an player isn’t in the game but when a player joins you can get an player instance

Where you can use player.UserId and player.name

1 Like

Yeah I get that part but I am just saying that I never knew that Player1 and Player2 had unique userids. I just thought they had the same ids as the one testing them.

So I should just use RemoveAsync() on these new keys to delete them from data store? Or just use if statements

You need to use player.userid to store data

1 Like

Whatever you like. Use remove async if you want to remove them permanently.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.