Other way to get the thumbnail of a user

This is my method:

local ThumbnailStringTemplate = "rbxthumb://type=%s&id=%s&w=%s&h=%s"
local ThumbnailNames = {
	[Enum.ThumbnailType.HeadShot] = "AvatarHeadShot",
	[Enum.ThumbnailType.AvatarThumbnail] = "Avatar",
	[Enum.ThumbnailType.AvatarBust] = "AvatarBust",
	[Enum.ThumbnailSize.Size48x48] = "48",
	[Enum.ThumbnailSize.Size60x60] = "60",
	[Enum.ThumbnailSize.Size100x100] = "100",
	[Enum.ThumbnailSize.Size150x150] = "150",
	[Enum.ThumbnailSize.Size180x180] = "180",
	[Enum.ThumbnailSize.Size352x352] = "352",
	[Enum.ThumbnailSize.Size420x420] = "420",
}

local function GetThumbnailByUserId(UserId: number, ThumbnailType: Enum.ThumbnailType, ThumbnailSize: Enum.ThumbnailSize): string
	return ThumbnailStringTemplate:format(ThumbnailNames[ThumbnailType], tostring(UserId), ThumbnailNames[ThumbnailSize], ThumbnailNames[ThumbnailSize])
end
7 Likes

or just save 20 lines and have your image load 0.2 milliseconds later

2 Likes

You can store this in a module

Players:GetUserThumbnailAsync is better

Why is it better? You can clearly see in the video that Players:GetUserThumbnailAsync is slower than my method

Oh bruh my bad, I didn’t see the video the first time. Okay that’s actually interesting. Why is it faster??

I assume because it’s just adding things to a string instead of making a call to the roblox website like the Roblox method does.

You can see that my method immediately gets the thumbnail and also immediately changes the image

-- This code does the same thing as in the video
task.wait(5)

local ThumbnailStringTemplate = "rbxthumb://type=%s&id=%s&w=%s&h=%s"

local ThumbnailNames = {
	[Enum.ThumbnailType.HeadShot] = "AvatarHeadShot",
	[Enum.ThumbnailType.AvatarThumbnail] = "Avatar",
	[Enum.ThumbnailType.AvatarBust] = "AvatarBust",
	[Enum.ThumbnailSize.Size48x48] = "48",
	[Enum.ThumbnailSize.Size60x60] = "60",
	[Enum.ThumbnailSize.Size100x100] = "100",
	[Enum.ThumbnailSize.Size150x150] = "150",
	[Enum.ThumbnailSize.Size180x180] = "180",
	[Enum.ThumbnailSize.Size352x352] = "352",
	[Enum.ThumbnailSize.Size420x420] = "420",
}

local RandomUserIds = {4486178348, 845495652, 1460163254, 4049582782, 2772121961, 5277769812}

local ImageLabel1 = script.Parent.ImageLabel1
local ImageLabel2 = script.Parent.ImageLabel2

local function GetThumbnailByUserId(UserId: number, ThumbnailType: Enum.ThumbnailType, ThumbnailSize: Enum.ThumbnailSize): string
	return ThumbnailStringTemplate:format(ThumbnailNames[ThumbnailType], tostring(UserId), ThumbnailNames[ThumbnailSize], ThumbnailNames[ThumbnailSize])
end

coroutine.wrap(function()
	
for i = 1,1000 do
	
	local RandomuserId = RandomUserIds[math.random(#RandomUserIds)]
	
	ImageLabel1.Image = GetThumbnailByUserId(RandomuserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	
end

print("My method ended")

end)()

coroutine.wrap(function()
	
for i = 1,1000 do
	
	local RandomuserId = RandomUserIds[math.random(#RandomUserIds)]
	
	ImageLabel2.Image = game.Players:GetUserThumbnailAsync(RandomuserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	
end

print("Roblox method ended")

end)()

Yes, something like that. :watermelon: :watermelon: :watermelon: :watermelon: :watermelon: :watermelon: :watermelon: :watermelon:

Why would you write extra code when it is right here?

1 Like

Right where? :eyes: :eyes: :eyes: :eyes: :eyes: :eyes: :eyes: :eyes: :eyes: :eyes: :eyes:

Just create an module that stores the function like this:

local ThumbnailStringTemplate = "rbxthumb://type=%s&id=%s&w=%s&h=%s"

local ThumbnailNames = {
	[Enum.ThumbnailType.HeadShot] = "AvatarHeadShot",
	[Enum.ThumbnailType.AvatarThumbnail] = "Avatar",
	[Enum.ThumbnailType.AvatarBust] = "AvatarBust",
	[Enum.ThumbnailSize.Size48x48] = "48",
	[Enum.ThumbnailSize.Size60x60] = "60",
	[Enum.ThumbnailSize.Size100x100] = "100",
	[Enum.ThumbnailSize.Size150x150] = "150",
	[Enum.ThumbnailSize.Size180x180] = "180",
	[Enum.ThumbnailSize.Size352x352] = "352",
	[Enum.ThumbnailSize.Size420x420] = "420",
}

local Thumbnail = {}

function Thumbnail.GetThumbnailByUserId(UserId: number, ThumbnailType: Enum.ThumbnailType, ThumbnailSize: Enum.ThumbnailSize): string
	return ThumbnailStringTemplate:format(ThumbnailNames[ThumbnailType], tostring(UserId), ThumbnailNames[ThumbnailSize], ThumbnailNames[ThumbnailSize])
end

return Thumbnail

pretty sure yours doesn’t return the isReady Boolean flag which Players:GetUserThumbnailAsync() does. Correct me if I’m wrong.

I mean, yeah, this is a good system. I tend to use stuff like this as a backup in case the API for it fails, usually in an xpcall.

My method doesn’t need isReady because it’s immediately ready

Players:GetUserThumbnailAsync() returns a string in the exact same format as your system does, except it also returns the isReady flag to tell you if the thumbnail is ready to use or not, i.e. if it will load.

Yours won’t always be ‘immediately ready’, the same way the one returned from Players:GetUserThumbnailAsync() won’t always be immediately ready.

Therefore, it does not have the capability for the same level of security as Players:GetUserThumbnailAsync(), hence why I said it’s good as a backup in case the API network request fails.

1 Like

Ok, but shouldn’t this be in #resources:community-resources?

This is my first post idk what #resources:community-resources is

It’s the category intended for resources like this.

Your post could get removed if you don’t change it.

Based on your arbitrary benchmarks? So you are counting that the server will have no spikes/latency and it will always load the thumbnails immediately?

I don’t see the point in storing every thumbnail type and size when there aren’t many cases, maybe none, where the thumbnail needs to be changed or where a different one is used for each player. You usually always use the same type and size for everyone.

Maybe I’m wrong, but I think it’s not worth creating a module and storing all of these things when you could just use GetUserThumbnailAsync which does the exact same thing or directly put the link and change the user ID to the very few instances that show an user profile picture.

ImageLabel.Image = "https://www.roblox.com/headshot-thumbnail/image?userId="..Player.UserId.."&width=420&height=420&format=png"
3 Likes