if typing == false then
if pcall(function() game.Players:GetUserIdFromNameAsync(usernameField.Text) end) == nil then
errorLine.Visible = true
errorMessage.Text = "Error: '"..usernameField.Text.."' is not a valid username."
errorMessage.Visible = true
end
end
end)```
Nothing comes in the output. Any ideas?
Using the pcall function returns multiples values (a tuple), a bool indicating if the execution of the function was successful or not, and the results of the function or the error message.
Example:
local success, result = pcall(function ()
return game.Players:GetUserIdFromNameAsync(usernameField.Text)
end)
if success then
print(result) -- The UserId
else
warn(result) -- The error message
end
In your if statement, you’re checking if the first value is nil, which it will never be.
In your pcall function, your also not returning the UserId you got from their name.
local usernameField = script.Parent
local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")
userInput.InpuBegan:Connect(function(input, typing)
if typing then
return
end
local success, result = pcall(function()
return players:GetPlayerByUserId(usernameField.Text)
end)
if success then
if result then
local player = result
print(player.Name)
end
else
warn(result)
errorLine.Visible = true
errorMessage.Text = "Error: '"..usernameField.Text.."' is not a valid user ID."
errorMessage.Visible = true
end
end)
I filled in the blanks a little but I believe this is what you were trying to do. If you want to find a player based off their username instead then that’s a simple change.
local usernameField = script.Parent
local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")
userInput.InpuBegan:Connect(function(input, typing)
if typing then
return
end
local success, result = pcall(function()
return players:FindFirstChild(usernameField.Text)
end)
if success then
if result then
local player = result
print(player.Name)
end
else
warn(result)
errorLine.Visible = true
errorMessage.Text = "Error: '"..usernameField.Text.."' is not a valid username."
errorMessage.Visible = true
end
end)