Check if player not in game or in game exists

How would I figure out if a player exists even if they aren’t in the game?

ex:

--"pl" not a person that exists with this name

--"LEO_1964" a username so true

Check :GetUserIdFromNameAsync out.

Wrap the request in a pcall and check if the request is successful or fails. If it succeeds, you know that player exists.

Additionally, from a quick google search I found this post. Finding if a Roblox user exists

Example usage:

local UsernameToSearch = "yasir10001"
local Success = pcall(function()
	game:GetService("Players"):GetUserIdFromNameAsync(UsernameToSearch)
end)

if Success then
	print("Player exists")
else
	print("Player is non-existent")
end
1 Like

It’s also important to note that your solution won’t be able to distinguish between if Roblox throws an error because of an HTTP thing, or if the player simply doesn’t exist. You also should use the second argument of the pcall to find the username in case it has to be referenced later:

local usernameToSearch = 'hi'
local success, username = pcall(game:GetService('Players').GetUserIdFromNameAsync, game:GetService('Players'), usernameToSearch))
-- if the pcall fails, the error message will go to the username variable

if not success then
    if username:lower():find('unknown user') then
        print('User doesn\'t exist!')
    else
        print('Failed to check if player exists because',username)
    end
end
2 Likes

Thank you @yasir10001 and @7z99

1 Like