How do I make a username input instead of userId input?

Basically, I have a textbox where if you put in the ID of a user, the user appears as the dummy. But how, instead of plugging in the userid of the user, how would I go about making the username the prompt you have to type into the box?

Video if you are confused:

Humanoid Appearance Scripts:

Remote event script:

-- if the text in the textbox = a number, the avatar of that number(userid) will be spawned as the dummy.

local Plr = game.Players.LocalPlayer
local textbox = Plr.PlayerGui:WaitForChild("Main").MainFrame.UsernameIdPicker


local Event = game.ReplicatedStorage:WaitForChild("CharacterChooser")



textbox.FocusLost:Connect(function(Send: boolean)
	if Send and tonumber(textbox.Text) then
		Event:FireServer(tonumber(textbox.Text))
	end
end)

Dummy changer script:

-- assigns the dummy the value(userid) from the RemoteEvent script.

for _, v in pairs (game.ServerStorage:GetDescendants())do
	if v.Name == 'Dummy' then
		Remote.OnServerEvent:Connect(function(Player: Player, UserId: number) 
			local Desc = game.Players:GetHumanoidDescriptionFromUserId(UserId)


			v.Humanoid:ApplyDescription(Desc)
		end)
	end
end

If any knows how to fix this, please let me know.

You could use something like this line

local Desc = game.Players:GetHumanoidDescriptionFromUserId(game.Players:GetNameFromUserIdAsync(NAME))

how would I get the NAME part of the script though, what lines of code do I put in to tell the textbox what a username is?

To tell userIDs apart from names?

if tonumber(Input) then
 -- UserID
else
 -- Name
end

I know im literally dumb i thought that said GetUserIdFromUserIdAsync even though that would make 0 sence,

actually this is very werid to me, I’m still a little confused, can you please walk through like the first step, after that I think I got it from there.

You have to define Input, so TextBox.Text, or whatever you want it as

basically, to number will return a number value from a string (i.e. “12345” → 12345), and will return nil if it can’t be turned directly into a number (i.e. “ABC123” → nil)

So if it turns into a number, then it is a user ID, if not, then it is a name

Dummy changer script:

for _, v in pairs (game.ServerStorage:GetDescendants())do
	if v.Name == 'Dummy' then -- change part to the name you want to look for
		Remote2.OnServerEvent:Connect(function(Player: Player, Username: name) 
			local Desc = game.Players:GetHumanoidDescriptionFromUserId(game.Players:GetNameFromUserIdAsync(Username))



			v.Humanoid:ApplyDescription(Desc)
		end)
	end
end

Remote event script:

local Event2 = game.ReplicatedStorage:WaitForChild("IsUsername")



textbox.FocusLost:Connect(function(Send: boolean)
	if Send and tonumber(textbox.Text) then
		Event:FireServer(tonumber(textbox.Text))
	else
		Event2:FireServerx(textbox.Text)
	end
end)

still won’t work but I am completely stumped.

Don’t know if you’ve found this yet, but you accidentally put an “x” in Event2:FireServerx.

Use the Players:GetUserIdFromNameAsync() function. This will return an UserId, which you can use to get a HumanoidDescription.

Your trying to use the method that gets the players name by userid. You also don’t have to use 2 remotes if they are different, all you need todo is check what it is
I believe the solution your looking for is
This is how I would do it, I am unsure if this is really efficient or readable so I left comments and I apologize if this didn’t solve anything.

-- Remote script
local Event = nil -- Replace nil with your event.
textbox.FocusLost:Connect(function(Send: boolean)
	if Send and textbox.Text then
		Event:FireServer(textbox.Text)
	end
end)
-- Main
local Players = game:GetService("Players") -- Better to declare service names instead of game.Players
local ServerStorage = game:GetService("ServerStorage") -- I believe this returns nil if your script isn't in ServerStorage or ServerScriptService?
local Remote = nil -- Replace nil with your remote

local function GetAppearanceFromPlayer(input) -- Gets the appearance using 2 methods and checking what type of input was given
      local Appearance = nil
      local UserID = nil
      if typeof(input) == "string" then -- unsure if this is how typeof works, I never use it. Please fix what it is equal to if I'm incorrect.
          UserID = Players:GetUserIdFromNameAsync(input) -- This only applies to account name and not display names
         elseif typeof(input) == "number" then
          UserID = Players:GetPlayerByUserId(input) -- You can also use UserIds to get the appearance!
      end

      if UserID then
          Appearance = Players:GetHumanoidDescriptionFromUserId(UserID) -- Checks if the UserID was found, and gets the appearance 
      end

      return Appearance -- Only returns nil if the UserID never was a players id
end


Remote.OnServerEvent:Connect(function(Player: Player, User: string | number) -- Replaced Username: name -> User: string | number which allows the usage of userids and usernames
       for _, value in pairs(ServerStorage:GetDescendants()) do
             if value:IsA("Model") and value.Name == "Dummy" then
                local Character = value
                local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
                local Description = GetAppearanceFromPlayer(User)
                if Character and Humanoid and Description then
                    Humanoid:ApplyAppearance(Description)
                end
             end
       end
end)

If you have any errors with this script please reply back.
EDIT: To explain what this is doing is, when you fireserver the Remote, it checks the entire ServerStorage for models with the name Dummy which I assume your looking for. It then gets the Character and Humanoid then calls the GetAppearanceFromPlayer function I created with the textbox’s text you sent, which then returns the UserID even if its string or number. And if the Character, Humanoid, and Description are returned not as nil then it will apply the humanoid. (This might be a little confusing, I believe someone could make this a little easy to understand?)

I ignored the messages above so correct me if I’m wrong

client:

local Plr = game.Players.LocalPlayer
local textbox = Plr.PlayerGui:WaitForChild("Main").MainFrame.UsernameIdPicker


local Event = game.ReplicatedStorage:WaitForChild("CharacterChooser")



textbox.FocusLost:Connect(function(Send: boolean)
	if Send then
		Event:FireServer(textbox.Text)
	end
end)

server

Remote.OnServerEvent:Connect(function(Player: Player, Name: string)
	pcall(function()
		local UserId = game.Players:GetUserIdFromNameAsync(Name)
		local Desc = game.Players:GetHumanoidDescriptionFromUserId(UserId)

		for _, v in pairs (game.ServerStorage:GetDescendants())do
			if v.Name == 'Dummy' then
				v.Humanoid:ApplyDescription(Desc)
			end
		end

		Desc:Destroy()
	end)
end)
2 Likes