Need help with finding user using GetPlayerByUserId

  1. What do you want to achieve? Fix finding user

  2. What is the issue? In the function, it doesn’t want to find the ‘user’ (yes, it works if there isn’t the local user)

  3. What solutions have you tried so far? Tried some fixes. None worked.

local function douianimation(player)
		local number = 4
		local selectedFriends = selectplayerfriends(player, number)
		local ui = ReplicatedStorage.templates.Publish:Clone()
		ui.Parent = player.PlayerGui

		for _, userId in pairs(selectedFriends) do
			for _, Review in pairs(ui.Frame.Reviews:GetChildren()) do
				if Review.Contents.Headshot.Image == "rbxassetid://0" then
					TweenService:Create(Review.UIScale, TweenInfo.new(0.1), {Scale = 1}):Play()
					local success, thumbnail = pcall(function()
						return Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
					end)

					if success then
						local user = Players:GetPlayerByUserId(userId)
						Review.Contents.Headshot.Image = thumbnail
						Review.Contents.User.Text = "@" .. user.Name
					end
					break
				end
			end
		end
	end

Can you please post the “selectplayerfriends” function code as well as what is calling this function?

1 Like

Are there any errors?

if not do what the person above said, but also put some prints in the if success then to tell if it actually runs

1 Like
local function getplayerfriends(player)
		local friendsId = player.UserId
		local PlayersFriends = {}

		local success, page = pcall(function() return Players:GetFriendsAsync(friendsId) end)
		if success then
			repeat
				local info = page:GetCurrentPage()
				for i, friendInfo in pairs(info) do
					table.insert(PlayersFriends, friendInfo)
				end
				if not page.IsFinished then
					local advanceSuccess, advanceError = pcall(function() return page:AdvanceToNextPageAsync() end)
					if not advanceSuccess then
						warn("Error advancing page:", advanceError)
						break
					end
				end
			until page.IsFinished
		else
			warn("Failed to fetch friends for user:", player.Name)
		end

		return PlayersFriends
	end

	local function selectplayerfriends(player, number)
		local PlayersFriends = getplayerfriends(player)
		local selectedFriends = {}

		for i = 1, math.min(number, #PlayersFriends) do
			table.insert(selectedFriends, PlayersFriends[i].Id)
		end

		return selectedFriends
	end

everything else works other than finding the user

Can you post the function that actually calls “douianimation”?

1 Like
local module = {}
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local allreviews = require(ReplicatedStorage.modules.main.allreviews)

module.mainPublish = function(player)
	local function getplayerfriends(player)
		local friendsId = player.UserId
		local PlayersFriends = {}

		local success, page = pcall(function() return Players:GetFriendsAsync(friendsId) end)
		if success then
			repeat
				local info = page:GetCurrentPage()
				for i, friendInfo in pairs(info) do
					table.insert(PlayersFriends, friendInfo)
				end
				if not page.IsFinished then
					local advanceSuccess, advanceError = pcall(function() return page:AdvanceToNextPageAsync() end)
					if not advanceSuccess then
						warn("Error advancing page:", advanceError)
						break
					end
				end
			until page.IsFinished
		else
			warn("Failed to fetch friends for user:", player.Name)
		end

		return PlayersFriends
	end

	local function selectplayerfriends(player, number)
		local PlayersFriends = getplayerfriends(player)
		local selectedFriends = {}

		for i = 1, math.min(number, #PlayersFriends) do
			table.insert(selectedFriends, PlayersFriends[i].Id)
		end

		return selectedFriends
	end

	local function douianimation(player)
		local number = 4
		local selectedFriends = selectplayerfriends(player, number)
		local ui = ReplicatedStorage.templates.Publish:Clone()
		ui.Parent = player.PlayerGui

		for _, userId in pairs(selectedFriends) do
			for _, Review in pairs(ui.Frame.Reviews:GetChildren()) do
				if Review.Contents.Headshot.Image == "rbxassetid://0" then
					TweenService:Create(Review.UIScale, TweenInfo.new(0.1), {Scale = 1}):Play()
					local success, thumbnail = pcall(function()
						return Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
					end)

					if success then
						local review = allreviews[math.random(1, #allreviews)]
						local user = Players:GetPlayerByUserId(userId)
						Review.Contents.Headshot.Image = thumbnail
						Review.Contents.Description.Text = review.Review 
						Review.Contents.User.Text = "@" .. user.Name
					end
					break
				end
			end
		end
	end

	douianimation(player)
end

return module

Are you sure that the if success then runs? If it does run then you should be getting an error, in that case what is the error?

If there is no error, please put a print in the if success then to see if it runs.
And even put one before to print success

1 Like

if success then works, i put print(“success”) and it printed, it just doesn’t find the user, other than the Review.Contents.User.Text works fine.

1 Like

so its just not displaying the name of the user?

1 Like

yep, just saw that if i try to do the text, it does a error (image below)

1 Like

can you try printing the user? try to see if it’s nil.

1 Like

That means user is nil and there’s a reason why, try printing userid for each of the friends.

1 Like

userids of friends print, the user is nil

Can you print “selectedfriends” and tell me what it prints?

1 Like

prints the table of 4 random friends.
image

from a quick search it turns out this function only returns a player if they are in the game.

You can use this instead:
GetNameFromUserIdAsync

2 Likes

ohohohh I didn’t think about that, yeah that will work.

2 Likes

i’ve tried that before, this is the output.

Can I see the code where you used it like that? I assume you just set user to said function, but that wont work as you reference it as user.Name. Thats my guess at least, so try this?

local module = {}
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local allreviews = require(ReplicatedStorage.modules.main.allreviews)

module.mainPublish = function(player)
	local function getplayerfriends(player)
		local friendsId = player.UserId
		local PlayersFriends = {}

		local success, page = pcall(function() return Players:GetFriendsAsync(friendsId) end)
		if success then
			repeat
				local info = page:GetCurrentPage()
				for i, friendInfo in pairs(info) do
					table.insert(PlayersFriends, friendInfo)
				end
				if not page.IsFinished then
					local advanceSuccess, advanceError = pcall(function() return page:AdvanceToNextPageAsync() end)
					if not advanceSuccess then
						warn("Error advancing page:", advanceError)
						break
					end
				end
			until page.IsFinished
		else
			warn("Failed to fetch friends for user:", player.Name)
		end

		return PlayersFriends
	end

	local function selectplayerfriends(player, number)
		local PlayersFriends = getplayerfriends(player)
		local selectedFriends = {}

		for i = 1, math.min(number, #PlayersFriends) do
			table.insert(selectedFriends, PlayersFriends[i].Id)
		end

		return selectedFriends
	end

	local function douianimation(player)
		local number = 4
		local selectedFriends = selectplayerfriends(player, number)
		local ui = ReplicatedStorage.templates.Publish:Clone()
		ui.Parent = player.PlayerGui

		for _, userId in pairs(selectedFriends) do
			for _, Review in pairs(ui.Frame.Reviews:GetChildren()) do
				if Review.Contents.Headshot.Image == "rbxassetid://0" then
					TweenService:Create(Review.UIScale, TweenInfo.new(0.1), {Scale = 1}):Play()
					local success, thumbnail = pcall(function()
						return Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
					end)

					if success then
						local review = allreviews[math.random(1, #allreviews)]
						local name = Players:GetNameFromUserIdAsync(userId)
						Review.Contents.Headshot.Image = thumbnail
						Review.Contents.Description.Text = review.Review 
						Review.Contents.User.Text = "@" .. name
					end
					break
				end
			end
		end
	end

	douianimation(player)
end

return module
2 Likes