I have a script where it displays the users avatar, username and userid when I enter a username into a textbox. And when I enter an account username that doesnt exist, for example if i enter "“entuwfnwu” into the textbox, it will look for the player and try to grab its userid to display it. But that player doenst exist and breaks the code, how would I get around this?
Heres my code:
local player = game.Players.LocalPlayer
local frame = script.Parent
local username = script.Parent.Parent.Username
local userImage = frame.UserImage.UserImage
local userNameName = frame.Username
local userId = frame.UserId
while wait(0.1) do
userImage.Image = "http://www.roblox.com/Thumbs/Avatar.ashx? x=150&y=150&Format=Png&username="..username.Text
userNameName.Text = username.Text
userId.Text = game.Players:GetUserIdFromNameAsync(username.Text)
end
local player = game.Players.LocalPlayer
local frame = script.Parent
local username = script.Parent.Parent.Username
local userImage = frame.UserImage.UserImage
local userNameName = frame.Username
local userId = frame.UserId
local Name_to_ID = nil
while wait(0.1) do
Name_to_ID = game.Players:GetUserIdFromNameAsync(username.Text)
if game.Players:FindFirstChild(username.Text) then
userImage.Image = "http://www.roblox.com/Thumbs/Avatar.ashx? x=150&y=150&Format=Png&username="..username.Text
userNameName.Text = username.Text
userId.Text = Name_to_ID
end
end
The easiest work-around would be to wrap the ‘GetUserId…’ in a pcall so it doesn’t break the code:
local PlrId
success = pcall(function()PlrId= game.Players:GetUserIdFromNameAsync(username.Text) end)
if success then userId.Text = PlrId else warn("Player doesn't exist!") end