Player Name Input In TextBox Won't Show player pfp in image label

im working on a plugin where you can load characters of players, im working on the textbox rn, but i encountered an error; i type theplayer’s name but it doesn’t even do anything nor show pfp

1. local box = script.Parent
2. local img = box.Parent:WaitForChild("PlayerImage")
3. local players = game:GetService("Players")

4. box.FocusLost:Connect(function(enterpressed, loser)
5. 	if enterpressed then
6. 		local player = players:FindFirstChild(box.Text)
7. 		if player then
8. 			img.Image = players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
9. 		end
10. end)

If this is a plugin, this won’t work.

There are no players underneath Players in studio, unless you’re in team create. And even then, you could only load the images of team create members


local box = script.Parent
local img = box.Parent:WaitForChild("PlayerImage")
local players = game:GetService("Players")

box.FocusLost:Connect(function(enterpressed, loser)
   if enterpressed then
        local success, userId = pcall(players.GetUserIdFromNameAsync, players, box.Text)  -- fetch the player's UserId from the given name
 		if success then -- check that the pcall was successful
 			img.Image = players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
        else
            warn(userId) -- warn the error message if something when wrong
 	    end
    end
end)

is it possible if it generates it even if i type their id? @HugeCoolboy2007

game:GetService("Players"):GetUserIdFromNameAsync(playerName)
This is what you’re looking for, I made the changes for you already. You just input the user’s name

local box = script.Parent
local img = box.Parent:WaitForChild("PlayerImage")
local players = game:GetService("Players")

box.FocusLost:Connect(function(enterpressed, loser)
	if enterpressed then
	local userid = game:GetService("Players"):GetUserIdFromNameAsync(box.Text)
 	if userid then
	img.Image = players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	end
end
end)

didn’t work, it gives me error of failed: unknown user

You sure you’re typing their username correctly? I tested it out and it works fine for me

You’ll have to make some sort of toggle for that (i.e. “click this button to search by userid”) then just do tonumber(box.Text) for the userid

Maybe try wrapping it in a pcall, async functions may occassionally fail since they send web requests.

Unless it’s failing everytime of course.

i typed id
122222222214141141412

im new to the call thingies
30

try it with their username instead

the name works but the id doesn’t work

so you want to search by id only?

no, i want to search with both

local box = script.Parent
local img = box.Parent:WaitForChild("PlayerImage")
local players = game:GetService("Players")

box.FocusLost:Connect(function(enterpressed, loser)
	if enterpressed then
		
	local isUserId = tonumber(box.Text)
	if not isUserId then
	local userid = game:GetService("Players"):GetUserIdFromNameAsync(box.Text)
 	if userid then
	img.Image = players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	end
	else
	img.Image = players:GetUserThumbnailAsync(isUserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	end
end
end)

The problem with that is the fact that someone can have a username entirely composed of numbers

oh so how to fix it?
155555555555

It’s really not too complicated:

-- Example

local searchByUserId = false
ToggleButton.MouseButton1Click:Connect(function()
   searchByUserId = not searchByUserId -- if the value is 'false', set it to true and vice versa
end)

box.FocusLost:Connect(function(enterpressed, loser)
   if enterpressed then
        local userId = 0
        if not searchByUserId then -- search by username
            local success, output = pcall(players.GetUserIdFromNameAsync, players, box.Text)  -- fetch the player's UserId from the given name
 		    if not success then -- check that the pcall was successful
                warn(userId) -- warn the error message if something when wrong
                return
            else
                userId = output -- set the userid to the output
 	        end
        else
            userId = tonumber(box.Text) -- the user switched to search by user ids
        end

       img.Image = players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
    end
end)

Keep in mind that when the user switches the search functionality, you should give them a visual indicator of that (like: “Now searching by UserId”)

doesn’t work but i aprreciate ur try

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.