How to get player from text

Hello, I am trying to get the player from a textbox and my script isnt working I keep getting this error Players:GetUserIdFromNameAsync() failed: Unknown user
here is my script

script.Parent.MouseButton1Click:Connect(function()
	local text = script.Parent.Parent.PlayerText.Text
	local player = game.Players:GetUserIdFromNameAsync(text)
	print(player)
end)

If there’s no text or there’s no user with that name it will return that error

I typed in my own user it should 100% work

Can you assure that you’re taking the text from the rightful place?

You mean by making sure its the right textbox yes I even changed the name of the textbox

This method requires an existing username in order to return an ID. Or else, it will throw an error.

You should first try and get the ID from the player object if the player exists in game by doing the following:

local id = -1;
if (game.Players:FindFirstChild(text)) then
     id = game.Players:FindFirstChild(text).UserId;
end

It’s faster then the GetUserIdFromNameAsync() method as it doesn’t have to send any requets to the server.

If you need to search for a player that is not in the game, you must wrap your request in a pcall() function to catch any errors thrown by the method and not crash your script.

local id = -1
pcall(function()
    id = Players:GetUserIdFromNameAsync(text)
end)

if (id ~= -1) then
    -- User exists and an id was returned
end

If the id is still -1, then the user was not found. If not, it’s a valid user and the id is returned by the method successfully.

The final code should look something like this:

local id = -1;

if (game.Players:FindFirstChild(text)) then
    id = game.Players:FindFirstChild(text).UserId;
else
   pcall(function()
       id = Players:GetUserIdFromNameAsync(text)
   end)
end

if (id ~= -1) then
    -- User exists and an id was returned
end
1 Like

It errored once again and I did a check to see what it errored on again once it is this error

  Players:GetUserIdFromNameAsync() failed: Unknown user  

heres the script

script.Parent.MouseButton1Click:Connect(function()
	local Name = script.Parent.Parent.PlayerText.Text
	local id = -1
	local success, erorr = pcall(function()
		id = game.Players:GetUserIdFromNameAsync(Name)
	end)
	
	if success then 
		print("success")
	elseif erorr then
		print(erorr)
	end
	if (id ~= -1) then
		print(id)
	end
end)

Edit would you like a copy of the gui?

That’s because the string in script.Parent.Parent.PlayerText.Text is not a valid user.

Could I get the string your passing into the method?

Wdym get the string im passing into?

script.Parent.Parent.PlayerText.Text

image

No, the username you’re using with the method. What’s the argument being passed it?

For example, game.Players:GetUserIdFromNameAsync("Kevblx"), Kevblx is the argument. The value for the function parameter.

In your case, what are you typing into script.Parent.Parent.PlayerText.Text

Kevblx image

Try the following:

local Name = script.Parent.Parent.PlayerText.Text
print(Name)

Check the value of Name.

image

Tha’s the issue. The name being passed in is not valid. There’s no valid username being stored in your Name parameter. Thus, the method generating the Unknown user exception is valid.

You need to make sure you’re referencing the correct textbox.

Also, I’m positive your problem is due to using a server script. You shouldn’t be using a server script inside the client. Use a local script and send a ban request to the server. The reason Name is coming up blank is due to the fact that the server cannot see any textbox edits the user makes. You need to fire a remote funtion or event to the server and give it the textbox value.

Use a local script to read the textbox value, and use a remote function or remote event to process the player name on a server script.

1 Like

Letme go test something really quick

Sure, just remember to do the following and it should work.

Have a LocalScript contain the mouse button click code. It should fire a remote event or remote function and pass in the textbox.Text as an argument. For Example:

RemoteHook:FireServer(textbox.Text)

On the server, have it listen for an event and use the game.Players:GetUserIdFromNameAsync(Name) code we discussed above inside a server script located on the server. The server should handle all the banning functionality.

Wait so this all in the localscript?

script.Parent.Parent.BanButton.MouseButton1Click:Connect(function()
   local Text = script.Parent.Text
   local player = game.Players:GetUserIdFromNameAsync(Text)
   print(player)
end)

Thinking that you should use a local script for buttons

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