How to get an image of a player from only a string of their username?

Hey so this should be a relatively short thread but, im making a game and whenever you get killed a GUI pops up saying “Person KO 'ed you” and an image of the user’s avatar should appear accept it doesnt. (I tested it with my friend to see if it worked)

I want to know how I can get the player’s avatar image with only a string of their username also keep in mind that this is a localscript.

any help is appreciated thanks!

image

local userid = game.Players[game.Players.LocalPlayer.LastHit.Value].UserID
workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.KPlayer.Value.Humanoid
script.Parent.Icon.Image = (game.Players:GetUserThumbnailAsync(game.Players.LocalPlayer.userid, Enum.ThumbnailType.Headshot, Enum.ThumbnailSize.Size180x180))```
2 Likes

If you only have a string that contains the player’s Username, you can first look through the Players service to check if the player is still in the game – if they are, call :GetUserThumbnailAsync using the UserId property of the player Instance that was found.

Based on the code you’ve provided, I’m not sure what “KPlayer” and “LastHit” are referring to, however, I presume that “LastHit” is an ObjectValue that stores a reference to the player that inflicted the final bits of damage to the LocalPlayer and that “KPlayer” does the same thing, except for that player’s Character. If this is the case, there would be no need to look through the Players service using a string because you have direct references to the Player Instance.

I would highly suggest separating your code across multiple lines as to ensure it remains organized and that certain Instances exist at the time they’re being referenced, preventing any errors from occurring. Here’s an example restructure of those three lines of code:

local Players = game:GetService("Players") -- References Players Service for easy access to LocalPlayer & methods of Players Service
local player = Players.LocalPlayer -- References LocalPlayer as to prevent repetitive game.Players.LocalPlayer indexing

local function updateIcon() -- Function that's activated whenever you want to update the icon

    local KPlayer = Player:WaitForChild("KPlayer") -- Check for these two values before continuing
    local LastHit = player:WaitForChild("LastHit")

    if KPlayer and LastHit then -- If the BaseValues(?) exist, we can continue

        local otherPlayer = LastHit.Value -- Accesses the value properties of these BaseValues
        local otherCharacter = KPlayer.Value
--[[ This could be simplified to reference the Character of "otherPlayer"
without utilizing the "KPlayer" value, but I'm referencing it this way
right now since I don't know for sure what the variables are actually referring to --]]

        if otherPlayer and otherCharacter then -- If there were values stored, then...

            local otherHumanoid = otherCharacter:FindFirstChildOfClass("Humanoid") -- Checks if the otherPlayer's Character has a Humanoid

            if otherHumanoid then -- If they do, then...

                local Icon = script.Parent.Icon
                Icon.Image = Players:GetUserThumbnailAsync(otherPlayer.UserId, Enum.ThumbnailType.Headshot, Enum.ThumbnailSize.Size180x180
-- Updates Image to the avatar of the other user so you don't end up seeing your own avatar

                local CurrentCamera = workspace.CurrentCamera
                local CameraSubject = CurrentCamera.CameraSubject

                CameraSubject = otherHumanoid
            end
        end
    end
end

alright ill try to implement using your advice

also “LastHit” is a String Value that gets the player’s username and “KPlayer” (the K is supposed to mean Killer) is an Object Value of the player’s avatar for the camera that spectates the player when the gui is active and the script is enabled

Thanks for the clarification there – in that case referencing LastHit.Value].UserID would not have been possible in the OP since that would have been trying to index “UserID” on the string.

By “player’s Avatar”, are you referring to the player Instance or their Character in the Workspace? I don’t think this would be referring to the ImageLabel that contains the image of their Avatar (since that wouldn’t be a very efficient usage of an ObjectValue, from my understanding).

local userId = game.Players:GetUserIdFromNameAsync(username)
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

Alright so ive tested it out again using your advice and code as a refrence ive come up with this yet still seems to not want to work.

also I should mention the script works by being activated by another script that checks for when a player dies, it then clones the gui into the PlayerGui and activates the script at the same time.

a bit messy but everything worked just not the image

anyways heres the code:

local Players = game:GetService("Players")
local player = Players.LocalPlayer 

	local KP= player:WaitForChild("KPlayer")
	local LH = player:WaitForChild("LastHit")
	local Kplr = LastHit.Value
	local KChar = KPlayer.Value

	if KPlayer and LastHit ~= nil then
		local H = KChar:FindFirstChildOfClass("Humanoid")

			if H then
				local Icon = script.Parent.Icon
				Icon.Image = Players:GetUserThumbnailAsync(Kplr.UserId, Enum.ThumbnailType.Headshot, Enum.ThumbnailSize.Size180x180)
			local CurrentCamera = workspace.CurrentCamera
			local CameraSubject = CurrentCamera.CameraSubject

			CameraSubject = H
		end
	end

Are you using BindableEvents for this or do you mean changing the Disabled property of the script? If the latter, I would advise against this because that means there would be no way to run the code again without duplicating the script since there’s no function you can call.

Remember that you wouldn’t be able to directly reference the UserId property this way because the value of LastHit is not referring to the player Instance that allows you to access their UserId.

Furthermore, you didn’t adjust the variables to align with the code you have now:

local KP = player:WaitForChild("KPlayer")
local LH = player:WaitForChild("LastHit")
local Kplr = LastHit.Value -- LastHit would need to become "LH"
local KChar = KPlayer.Value -- KPlayer would need to become "KP"

okay ive taken everything into considerationg and ive gotten the script to work finally!
using your advice and adding some snipits of code from @CoinTheKit it will now successfully display an image on the GUI

also ill be sure to try make the script triggered via event to make it a bit stronger like said

local Players = game:GetService("Players")
local player = Players.LocalPlayer 

	local KP = player:WaitForChild("KPlayer")
	local LH = player:WaitForChild("LastHit")
	local Kplr = LH.Value
	local KChar = KP.Value

local userId = game.Players:GetUserIdFromNameAsync(Kplr)
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

	if KP and LH ~= nil then
		local H = KChar:FindFirstChildOfClass("Humanoid")

			if H then
				local Icon = script.Parent.Icon
			Icon.Image = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
			local CurrentCamera = workspace.CurrentCamera
			local CameraSubject = CurrentCamera.CameraSubject

			CameraSubject = H
		end
	end

Just wanted to mention that this would be making unnecessary requests to the Roblox website – if the player is already in the game, it’s more reliable and less resource-intensive to first look through the Players Service and check if it can find any Instance with the name stored in the Kplr variable.

Because of the action that’s required for this code to be run (a player inflicting enough damage onto another player), it’s very likely that the other player is still in the game (there would be very few instances, if any, where this would not be the case).

You can always add a conditional statement that will then ask the Roblox website to access it if the player left as soon as the script made it to this part of the code.

alright thanks will take this into consideration aswell

1 Like

You can get the player headshot simply with a string,
https://www.roblox.com/headshot-thumbnail/image?userId=UserId&width=420&height=420&format=png

Simply replace “UserId” with your UserId.
It’s really useful because you can just put that into the Image property of an ImageLabel.