Getting the UserId of a player that is not in-game?

Hello, it’s me, Baw. I am currently making a script where when a player joins it gets a random friend of theirs, puts their username in a label and their UserThumbnail in an image label. The username part works perfectly fine but I’m struggling at the UserThumbnail because it’s giving me an error that says “Players:GetUserIdFromName() failed because the user does not exist”

Here’s the whole script.

local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(plr)
	local USERNAME = plr.Name
	
	local function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end
	
	local userId = Players:GetUserIdFromNameAsync(USERNAME)
	local friendPages = Players:GetFriendsAsync(userId)
	local usernames = {}
    for item, pageNo in iterPageItems(friendPages) do
	table.insert(usernames, item.Username)
	end
	
	local Friend = math.random(#usernames)
	game.Workspace.EvilFriendName.SurfaceGui.TextLabel.Text =  usernames[Friend]
	
	local FriendId = Players:GetUserIdFromNameAsync(Friend)
	local Type = Enum.ThumbnailType.HeadShot
	local Size = Enum.ThumbnailSize.Size353x353
	local data, isReady = Players:GetUserThumbnailAsync(userId, Type, Size)
	local imageLabel = game.Workspace.EvilFriend.SurfaceGui.ImageLabel
    imageLabel.Image = data

	
end)

The part the error is appearing in is:

	local FriendId = Players:GetUserIdFromNameAsync(Friend)

So… Is there a way to achieve getting the UserId of a player that is not in-game?

Note: I am new to scripting, sorry if I don’t understand some things!

This only refers to the random number of friends in your table, lets say you have 8 usernames in the table, math.random(#usernames) would return return a random index of the table 1-8, not the actual stored usernames. It seems like you correctly referenced the table in the text label

So just use the same reference usernames[Friend] for the GetUserIdFromNameAsync

local FriendId = Players:GetUserIdFromNameAsync(usernames[Friend])

3 Likes

Thank you so much for the help, now that I look at it this makes so much more sense than just “Friend”, I appreciate you took the time of your day to help me :D!

1 Like