UserId from username

yea that’s what i thought, that’s why i was asking if there was any other way to get the userid. but i really don’t know how i would cut down on them since i only use them when i need them.

all right i’ma try that ------

you could try taking longer to load the usernames on your leaderboards, like when you initially load all the names on server start, add a task.wait(1) for each entry in the leaderboard. (wait 1 second between each use of :GetUsername)

make sure you are using a name cache too aswell if you aren’t already, and if you’re still getting the issue, then try using an HTTPS api

what is a name cache?? is it just to save the names if you continuously use them?

1 Like

yeah, exactly. You save them in a dictionary so you don’t need to make all the querys again when the leaderboard refreshes.

ohh alright thank you so much, i’ll try this


local function getPlayerUserIdByUsername(username)
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Name == username then
            return player.UserId
        end
    end
    return nil
end

local username = "UsernameHere"
local userId = getPlayerUserIdByUsername(username)

if userId then
    print("User ID for ".. username .. " is: " .. userId)
else
    print("Player with username ".. username .. " not found.")
end

yeah but the issue is i’m trying to get the players userid while their offline

While their offline you want it to be adjusted yes or no

i mean like, i’m trying to get the userid of a player that’s offline

I mean you just did it right now lol

This script uses GetUserIdFromNameAsync method provided by the Players service to retrieve the User ID of a player by their username, even if they are not currently in the game.

Or do you want another method

You’re probably getting rate limited, I’ve ran into this issue before in my game too. To check if you are, try adding a warning when your pcall fails…

local success, err = pcall(function()
	TargetUserId = game:GetService("Players"):GetUserIdFromNameAsync(Target)
end)
if not success then
    warn(err)
end

You’ll get some type of “Too many requests” output if it’s a rate limit issue.

Since UserIds never change, you should cache the results you get to avoid having to request the website every time. If you already asked someone a question and knew the answer, you wouldn’t ask them again, so let’s recreate that in your code. An example can be seen below:

local Players = game:GetService("Players")

local userIdCache = {}

local function getUserIdFromName(name: string): number?
    -- check if user has already been cached
    if not userIdCache[name] then
        -- if not, attempt to get the user ID
        local success, result = pcall(function()
            return Players:GetUserIdFromNameAsync(name)
        end)

        if success then
            -- store the user ID in the cache
            userIdCache[name] = result
        else
            -- handle error (maybe you could make it retry this function after a while, etc?)
            warn(result)
        end
    end

    -- return cached ID
    return userIdCache[name]
end

Then, you can simply say:

local userId = getUserIdFromName(name)
if not userId then return end

this can let you circumvent the rate limiting issue. Though you need to be familiar with httpservice. You can find other topics about doing this (though in other languages, but you should do it in lua)

but if you manage to solve the rate limiting issue then don’t bother implementing this, its way easier to mitigate the use of the players’ method

caching the players userid was the solution thanks y’all

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.