Can I get the Player's UserId from OrderedDataStore?

Hey, so I am currently doing a Global Leaderboard system for my simulator and I just ran into a problem. I am trying to get the UserId form the top 3 players and then does userId’s to get the clothing and hats the only problem I have is, how do I get the UserId from an OrderedDataStore?

Here is the Script:

local TotalWheatODS = game:GetService("DataStoreService"):GetOrderedDataStore("TotalCash1")
 
local function Handler()
    local Success, Err = pcall(function()
        local Data = TotalWheatODS:GetSortedAsync(false, 100)
        local TotalWheatPage = Data:GetCurrentPage()
        for Rank, Data in ipairs(TotalWheatPage) do
            local Name = Data.key
            local TotalWheat = Data.value
            local NewObj = game.ReplicatedStorage:WaitForChild("lbFrame"):Clone()
            NewObj.Player.Text = Name
            NewObj.TotalWheat.Text = TotalWheat
			NewObj.Rank.Text = "#"..Rank
            NewObj.Position = UDim2.new(0, 0, NewObj.Position.Y.Scale + (0.08 * #game.Workspace.StatCoinBoard.lbGUI.Holder:GetChildren()), 0)
			NewObj.Parent = game.Workspace.StatCoinBoard.lbGUI.Holder
        end
    end)
    if not Success then
        error(Err)
    end
end

while true do
    for _,Player in pairs(game.Players:GetPlayers()) do
        TotalWheatODS:SetAsync(Player.Name, Player.Totalcash.Value)
    end
    for _,v in pairs(game.Workspace.StatCoinBoard.lbGUI.Holder:GetChildren()) do
        if v.Name == "lbFrame" then
			wait()
            v:Destroy()
        end
    end
    Handler()
    wait(250)
end

I tried looking in the developer API references but found nothing :confused:

Can you just reference the UserId property of the player, Player.UserId?

1 Like

You should be saving the UserId as the key for if a player changes their username. But there are functions you could use to get the UserId from the Name and the Name from the UserId.

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

1 Like

no the thing is that with orderded data stores your storing data and then getting it back.

right now I am trying the GetUserIdFromNameAsync function

@iiNemo is very correct that you should be using the UserId as the player’s key. By using a username as the key, a player could lose all their data if they do a name change.

1 Like

I am aware of that but if you are getting a UserId form the name using the GetUserIdFromNameAsync function I think the player would not lose any data.

In your case, since you are using an OrderedDataStore, the player’s data will not be erased( Unless you are getting player data from it ), but will instead become a new key. This means if the player changes their username, they will have two separate places on the leaderboard for each of their usernames.

1 Like

oh, I never knew that xD. How would I get the userId for ordered data stores than?

Instead of saving by the player’s username like so,

TotalWheatODS:SetAsync(Player.Name, Player.Totalcash.Value)

Save by the player’s UserId.

Example:

TotalWheatODS:SetAsync(Player.UserId, Player.Totalcash.Value)

This is extremely important to prevent glitches and data wipes.

1 Like

oh ok I will try, brb (30 charsss)