local success, err = pcall(function()
TargetUserId = game:GetService("Players"):GetUserIdFromNameAsync(Target)
end)
I may be stupid but, is there a reason you are getting the player by the user id? I mean from the remote event you’ve already got the player instance. Can’t you just do player.UserId or something?
yea there is, i’m saving a datastore by userid and the Target is the player the person is gifting. so the Player is the player donating and Target is the reciever.
Ah I think I see why thats happening, it probably breaks after some time due to all the task.spawns and while loops you are using. You can’t use too many datastore getasync/setasync or else it’ll stop setting the data and getting the data for that key.
Yeah sorry lol I didn’t really read your full script because jesus. But I did see you are using many while loops/task.spawn functions which may be the reason why your script slowly breaks. Reduce them.
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.
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
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
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.
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