Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I tell if an id is a real player id?

1 answer

You can use GetNameFromUserIdAsync which will return the players name associated with the given id or error if that player id is not found so it needs to be wrapped in a pcall

Pcall will run the given function with the passed arguments and return if the code executed without any errors and any returned arguments the function itself returned.

Example

local plrServ = game:GetService('Players')

local function isValidPlayerId(id)
	-- calls the function GetNameFromUserIdAsync which is passed the players service to remove the need for ':' and the id passed 
	local res, _ = pcall(plrServ.GetNameFromUserIdAsync, plrServ, id)
	return res -- we know the user id is valid if the code runs successfully 
end

print(isValidPlayerId(-213)) -- prints false
print(isValidPlayerId(213)) -- prints true

I hope this helps

This discussion is preserved as it was. New answers, comments, and votes are closed.