Save data in Ordered data store by User Id?

I have this global leaderboard created with Ordered Data Store that saves data by username that shows how many times they visited, how can I save it by User Id instead?

Here’s my current script for the ordered Data Store:

local Players = game:GetService(“Players”)
local ods = game:GetService(“DataStoreService”):GetOrderedDataStore(“Visits”)
local clickDetector = Instance.new(“ClickDetector”)
clickDetector.Parent = script.Parent
local canClick = true

function onPlayerAdded(player)
print(player.Name … " has entered the game")

ods:IncrementAsync(player.Name, 1)
end

–When a player joins, call the onPlayerAdded function
Players.PlayerAdded:connect(onPlayerAdded)

–Call onPlayerAdded for each player already in the game
for _,player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end

Change the key in

ods:IncrementAsync(player.Name, 1)

from player.Name to player.UserId

1 Like

How can i grab username from the UserId, so i can display the username on a gui?

i have had this problem before and what i did was have a seperate datastore that stores the players name as the value and the key is the userid and if the key and the name dont match it updates it HOWEVER there is a built in method this is it i think