Question about GetUserThumbnailAsync yield

Without using task.spawn(function() the code works totally fine but GetUsertThumbnailAsync causes a yield so when I spawn at my part it’s delayed and my orientation screws up. How do I fix this?


You can try to cache the thumbnail image as soon as the player joins and removes it if they leave, the only problem with this is that if they change the avatar mid game it will show the incorrect avatar. To fix that you could actually have it auto fills the cache if every player in the server. Since I am on mobile and writing this script would take forever when I am on my computer and if you haven’t figured it out I can write it for you.

I havn’t yet this has me pretty stumped, any help would be fantast.

Sorry this took a while but I was writing this on mobile

This is a module script in replicated storage

local Shared = {}
local Local = {}
local thumbnails = {}

local Players = game:GetService("Players")

local ThumbnailRefreshTime = 2 * 60 -- 2 minutes

function Local.LoadCache()
for _, Player in Players: GetPlayers() do
thumbnails[Player.UserId] = Players:GetUserThumbnailAsync() -- you would need to put in the properties yourself because I don't not remember them
end
end

function Local.PlayerAdded(Player)
thumbnails[Player.UserId] = Players:GetUserThumbnailAsync() -- same here you will need to the properties yourself I can't remember them.
end 

function Local.PlayerRemoving(Player)
thumbnails[Player.UserId] = nil
end 

function Shared.GetThumbnail(Player) -- Call this in whatever script you need the thumbnail for.
return thumbnails[Player.UserId]
end

function Shared.Start() -- have this function called in a server script
Players.PlayerAdded:Connect(Local.PlayerAdded)
Players.PlayerRemoving:Connect(Local.PlayerRemoving)
Local.LoadCache()
task.Spawn(function()
while task.wait(ThumbnailRefreshTime) do
Local.LoadCache()
end
end)
end

There may be a few spelling mistakes since I am writing this off by heart and on mobile so you will need to fix those. That should be it so if you have any questions about the code feel free to ask and if is the solution make sure the solution button below to give me a good dopamine hit.

I don’t really understand the issue here. But assuming you’re talking about the yield, it’s a pretty simple answer.

:GetUserAsync is a method that connects to the roblox server to get a value so it can return it (In this case, the Player’s thumbnail by their UserID). The yield comes from the roblox server delay itself, as the function will need to yield until it get’s an answer from the Roblox servers.

The only solution to have the script not yeild is either A. the aforementioned task.spawn() or B. The solution provided above by @MrNobodyDev

(Quick note, using task.spawn might break the script if you use the image right after the variable being assigned as there is the possibility it’ll be nil since the server has yet to respond)

1 Like

Yes, that’s the issue I’ve been having with task.spawn. The thumbnail immediately stops working as soon as I implement it.

Ah I see, I’ll have to try this out but I want to try to use the code I’ve written and find some workaround with that but I’ll def try and use this as reference for fixing it.

1 Like

Run all the code you provided in a normal code block (without the GetUserThumbnailPart) and write a custom fetchUserThumbnail function and replace that segment with that. On the fetchUserThumbnail function include only the required code to load a user thumbnail from the API / set it. Make that function run asynchronously without pausing the thread. There are multiple ways to do so, for example using task.spawn and running it in another thread, firing a bindable event, or having a constant number of active threads / workers at place waiting to receive a function call to process (if you need to load many images and don’t want to constantly create and delete threads).

Here’s an example:

--rest of the code you have above
PlayerName.Text = player.Name
setThumbnailImage(PlayerLabel, player.UserId)
--simplified async implementation
local function setThumbnailImage(label: ImageLabel, userId: number)
	task.spawn(function()
		label.Image = Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size60x60)
	end)
end

Basically you only set the image in the async part so it doesn’t impact the rest of the code (your normal code doesn’t wait for the image to load to continue execution).

Tried this didn’t work, nothing has worked so far.

Nevermind, I’ve solved it. Thanks for everyones advice though!