Converting Player Name Into Player Image

I cant figure out why this wont work, Its supposed to update everytime the stringvalue gets changed to a different name.

-- Variables
local players = game:GetService("Players")

-- Create a StringValue to hold the player name
local playerNameValue = script:FindFirstChild("PlayerName")

-- Function to update the ImageLabel with the specified player's avatar
local function updateImageLabel(playerName)
	-- If playerName is nil or empty, do nothing
	if not playerName or playerName == "" then
		return
	end

	local player = players:FindFirstChild(playerName)

	if player then
		-- Get the player's avatar image ID
		local userId = player.UserId
		local ThumbType = Enum.ThumbnailType.AvatarThumbnail
		local ThumbSize = Enum.ThumbnailSize.Size150x150

		local PlayerImage, isReady = players:GetUserThumbnailAsync(userId, ThumbType, ThumbSize)

		-- Set the ImageLabel's Image property to the player's avatar
		script.Parent.Image = PlayerImage
	end
end

-- Function to handle changes to the PlayerName value
local function onPlayerNameChanged()
	updateImageLabel(playerNameValue.Value)
end

-- Listen for changes to the PlayerName value
if playerNameValue then
	playerNameValue:GetPropertyChangedSignal("Value"):Connect(onPlayerNameChanged)

	-- Initial setup
	if playerNameValue.Value then
		updateImageLabel(playerNameValue.Value)
	end
else
	warn("PlayerName StringValue not found")
end

1 Like

May I ask, what kind of script this is (Local or Server) and where you put this script.

1 Like

Maybe try using this instead of using Property Changed Signal, use: “playerNameValue.Changed:Connect(onPlayerNameChanged)”, also check if the string value is actually being changed, yet again you might be using an external script that may be changing the string value such as a local script, where the server script will not detect the change of a client side changed value or the other script may be having some problems, but I don’t know what you’re using to change the value. or what way you implemented for it to be changed.

1 Like

Hello, I am using all scripts in this no local scripts, a script changes the stringvalue. Using .Changed didnt work. :sob:

I also tested with setting the stringvalue without a script and it didnt work.