Is it possible to get a random roblox username. Not in the server just in general a random roblox player.
You can generate a number between 1 and the current number of roblox users and use that as the ID. When using API though, you need to make sure the player exists using protected calls (because some users have been removed).
I don’t know how to get the most recent player, though there is certainly an endpoint to get that, since games like The New User Machine get new users (those games might just constantly poll though).
What exactly do you mean by “Not in the server just in general”? Please be concise when making posts.
function GetRandomName (range)
local Players = game:GetService "Players"
local ID = math.random(range or 1000000000)
local Name
pcall(function()
Name = Players:GetNameFromUserIdAsync(ID)
end)
return Name or GetRandomName()
end
This is what I mean, how do I check if the user is banned or not?
You can use a protected call. My first post elicits this and re-runs the function until it finds a user who isn’t banned.
local s, e = pcall(function()
game.Players:GetUserIdFromNameAsync(ID) -- If the player is banned then this function will error.
end)
if s then
-- Player is available, e is nil.
else
-- Player is banned / unavailable, e is the error message.
end
If you’re not already aware, the pcall
method runs the parsed function, if there is no error then it returns success: boolean (true), error: nil
, if there is an error it returns success: boolean (false), error: string
.
Adding on to this, to get a random user id you would need to have the total number of players. You could use binary search for this but it would probably be better to just get a random user ID from all users created before a certain time, like today for example:
So then pick a number between 1 and 4,244,085,920. Check if the user id is valid with a pcall. (One thing to note is your pcall should ideally tell whether the ID is invalid or if Roblox APIs are down.) If the user id is invalid, your code should probably pick a new random user id and check that one.
Here is some example code:
local MAX_USER_ID = 4244085920 -- You could update this number occasionally.
local MIN_USER_ID = 1 -- You could increase this because some of the first
-- accounts are ones like Roblox and John Doe, though
-- the chances of getting those are one out of a
-- billion so it basically doesn't matter.
local Players = game:GetService("Players")
local function getRandomUserNameAndUserId()
local userId
local userName
while not userName do
userID = math.random(MIN_USER_ID, MAX_USER_ID)
-- You could have some code to handle when Roblox is down (check if
-- the error message is a regular one for a deleted user or if it's
-- something else)
pcall(function()
userName = Players:GetNameFromUserIdAsync(userId)
end
end
return userName, userId
end
An important note is that this function yields. Depending on how many users are deleted it might yield for different amounts of time, but it probably yields on average for 1.2 * Players:GetNameFromUserIdAsync()'s average (that’s a very rough estimation).
I would also call this in a pcall like you should for stuff that requests things because it might error from a stack over flow (very very low chance).
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.