Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

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

Asked by 1 year 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
kingdom5 5316 Trusted Snack Break Moderation Voter Community Moderator
1 year ago
Edited 1 year 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