How to check if a Player ID is valid?

For instance, lets say I have the following script.
I know this kind of system would be terrible, it’s just an example. I just need to know how to check if a userid is valid or not, without the player being in the server.

local EpicPeople = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(Player)
Played.Chatted:Connect(function(Message)
Message = tonumber(Message)
EpicPeople:SetAsync("Player_"..Message), "EPIC PERSON") -- Store the mentioned playerid as EPIC PERSON.
-- How can I check if a number is a userid, or invalid. Without said player being in the server.
end)
end)

You would probably use Players:GetNameFromUserIdAsync()

local IdValid, IdNotValid = pcall(function()
   game.Players:GetNameFromUserIdAsync(Id)
end)

if IdValid then
-- Valid userid
end

This just tries to get the player’s name from the userid provided. If it does not find a name for the player with said userid, we will know that the userid is not valid.

https://developer.roblox.com/en-us/api-reference/function/Players/GetNameFromUserIdAsync

5 Likes