Check If A Player Is Non-Existent (Quick Question)

Hello, I am looking for a simple way to check if a user doesn’t exist through the following line of code:

local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))

Since I started a local server to test my game for two players it kind of messed up the system. Thanks if you could help <3

Simple. Just use FindFirstChild with the players name in game.Players. If it returns nil/false, then it doesnt exist.

Or you can use i,v in pairs and check every player, and see if it matches with the data.key. If none of them dont, the user doesnt exist.

1 Like

I don’t understand the issue.

Are you speaking about “check if the character is a player”?

local char = game.Players:GetPlayerFromCharacter(game.workspace.CharacterModel)
if Char then print("Player exists.") else print("Player doesn't exist.") end

If you have their user ID, you can simply use this API.

If you’re asking how to check if an account exists in ROBLOX, you can use this code:

local Players = game:GetService("Players")

local success, username = pcall(Players.GetNameFromUserIdAsync, Players, data.key)

if success and username then
    -- account exists, you may run whatever code like inside here
end

Keep in mind since this call makes a request to ROBLOX’s API, it may fail which is why you should wrap it around in a pcall(). If it fails, then the success boolean which is returned from the pcall will be false.

I’m pretty sure it would also fail if the account doesn’t exist, so the code should do what you want it to.

4 Likes