My admin panel user finding system wont work

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)
1 Like

As far as I know Players:FindFirstChild("NotBugle", true) should work fine replace NotBugle with you’re variable obviously the true should loop until the player is found which might not be ideal so you might want to look into code “promises”.

The problem is with the else statement, not the main part, the else statement is trying to find the player via a username using UserIdFromNameAsync but that errors if the user isnt real. that doesnt really matter but it’s annoying because it just spams the console. And when I tried using a pcall it just breaks the else statement entirely even if the user is real

Have you tried

local PlayerFromAsync = Players:UserIdFromNameAsync(playerName)

if PlayerFromAsync then
-- player not in server
else
-- player doesn't exist
end

This won’t work because as soon as It tries to grab the user id it will just error if the username isnt real.

(and yes I tried it just in case)

I just saw now you used :UserIdFromNameAsync() it should be :GetUserIdFromNameAsync()

tried this in studio and it worked:

local Players = game:GetService("Players")

local playerName = "thisplayerdoesntexistbecausethenameistolonganditusesincorrectcharacters[]%^&*&^%^&*(*&^%^"

local success, userId = pcall(function()
	return Players:GetUserIdFromNameAsync(playerName)
end)

if success and userId then
	print(userId)
else
	print("player doesnt exist")
end

The reason you where getting an error from your pcall is because UserIdFromNameAsync isn’t a method

I realized that I had UserIdFromNameAsync and changed it but I forgot to update it in the original post.

Just tried this and it works! Thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.