I tried to make it so on my admin panel if the username typed into the username box is a real user then it will either:
1. If the user is in the server it will grab their avatar headshot and will apply it to the image label and enable the buttons that allow them to be jailed or unjailed etc.
2. If the user is not in the server it will find their profile (using their username to get their ID) and grab their headshot that way (and disable the buttons because they can’t be used)
The problem is that when it grabs the user’s ID, if the username isn’t a real user, it will error. It still works and finds the user but it’s annoying when it spams the console with the error. I have tried to wrap it in a pcall but then the system won’t grab the user all together and only grabs the users in the server.
I know this post is pretty big and seems kinda stupid, but I’m new to coding on ROBLOX and I have been scratching my head at this for like an hour now.
My code is below.
Code
local S_PLAYERS = game:GetService("Players")
local usernameBox = script.Parent.Parent.Parent.PlayerName.UsernameBox
usernameBox.Changed:Connect(function()
local playerName = usernameBox.Text
local player = S_PLAYERS:FindFirstChild(playerName)
if player then
-- Fetch the thumbnail
local userId = player.UserId
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size100x100
local content, isReady = S_PLAYERS:GetUserThumbnailAsync(userId, thumbType, thumbSize)
-- Set the ImageLabel's content to the user thumbnail
local imageLabel = script.Parent
imageLabel.Image = content
-- If the buttons are disabled then re-enable them because they can be used
local cmdFrame = script.Parent.Parent.Parent.CommandFrame
local buttons = cmdFrame:GetDescendants()
for i, v in pairs(buttons) do
if v:IsA("Frame") then
v.Visible = true
end
end
else
local playerName = usernameBox.Text
local success, userId = pcall(function()
return S_PLAYERS:UserIdFromNameAsync(playerName)
end)
if success and userId then
warn("Player has not been detected in server!")
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size100x100
local content, isReady = S_PLAYERS:GetUserThumbnailAsync(userId, thumbType, thumbSize)
local imageLabel = script.Parent
imageLabel.Image = content
-- Disable other buttons because they cant be used when user isnt in game
local cmdFrame = script.Parent.Parent.Parent.CommandFrame
local buttons = cmdFrame:GetDescendants()
for i, v in pairs(buttons) do
if v:IsA("Frame") then
v.Visible = false
end
end
else
warn("Player not found, either the user does not exist or there was an error searching.")
end
end
end)