Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by 6 years ago

How can I tell if an id is a real player id? I'm making a gui where you can enter a player id and be morphed into the player.

1 answer

Log in to vote
7
Answered by 6 years ago
Edited 6 years ago

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

Ad

Answer this question