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)
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
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)
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.
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.
script.Parent.Parent.BanButton.MouseButton1Click:Connect(function()
local Text = script.Parent.Text
local player = game.Players:GetUserIdFromNameAsync(Text)
print(player)
end)