Well im getting this error
Argument 1 missing or nil
which leads to
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
The key is not the UserId of a player as when you set the Data of the player you have player.UserId … “-time” for GetNameFromUserIdAsync() to work it has to be a straight UserId without* anything else
local userName = Data.key
also make the D in Data lowercase it will error if not also you previously defined the name of the player on the leaderboard so this line of code is unnecessary
So I made some changes, making the key only the UserId. Now, I get no errors, but it still doesn’t work. Here is my new code:
local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetOrderedDataStore("timeDataStore".."-time")
local function updateLeaderboard()
local success, errorMessage = pcall(function()
local Data = WinsLeaderboard:GetSortedAsync(false, 5)
local WinsPage = Data:GetCurrentPage()
for Rank, data in ipairs(WinsPage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local Time = data.value
local isOnLeaderboard = false
for i, v in pairs (game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
print(userName)
if Time and isOnLeaderboard == false then
local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
newLbFrame.Player.Text = Name
newLbFrame.Time.Text = Time
newLbFrame.Rank.Text = "#"..Rank
newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + .08 * #game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren(), 0)
newLbFrame.Parent = game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder
end
end
end)
end
while true do
for _, player in pairs (game.Players:GetPlayers()) do
WinsLeaderboard:SetAsync(player.UserId .. "-time", player.leaderstats.TimePlayed.Value)
end
for _, frame in pairs (game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
frame:Destroy()
end
updateLeaderboard()
print("Updated Leaderboard")
wait(30)
end
Try running it without the pcall. This way, it will show you the error, rather than erring silently (if an error occurs in a pcall, it doesn’t do anything)
This adds values to the stores with strings as their keys, not ints. When it runs, it tries to convert the string to an int but can’t because it’s only letter, so it returns nil (I think). Because of this, the get user from userid function will error because the first argument is nil.
If there is nothing else saved to the data store, you could try changing its name and adding this code:
However, I am saving my data as an int. For reference, here is my data saving script:
local DataStoreService = game:GetService("DataStoreService")
local MarketplaceService = game:GetService("MarketplaceService")
local timeDataStore = DataStoreService:GetDataStore("timeDataStore")
local user
game.Players.PlayerAdded:Connect(function(player)
user = player
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local timePlayed = Instance.new("IntValue")
timePlayed.Name = "TimePlayed"
timePlayed.Parent = leaderstats
local data
local success, errormessage = pcall(function(player)
data = timeDataStore:GetAsync(user.UserId)
end)
if success then
timePlayed.Value = data
else
warn("There was a serious error loading data. They have been kicked to prvent dataloss. Error message:".. errormessage)
user:Kick("There was a critical error loading your data. We had to kick you. Please provide this to the developer if this occurs again: ".. errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
timeDataStore:SetAsync(user.UserId, player.leaderstats.TimePlayed.Value)
end)
if success then
print("Data has been saved!")
else
print("There was an error when saving data.")
print(errormessage)
end
end)
local amount = 1
local timename = "TimePlayed"
while true do
wait(60)
for i,v in pairs (game.Players:GetPlayers()) do
if v:FindFirstChild("leaderstats") then
v.leaderstats[timename].Value = v.leaderstats[timename].Value + amount
end
end
local success, errormessage = pcall(function()
timeDataStore:SetAsync(user.UserId, user.leaderstats.TimePlayed.Value)
end)
if success then
print("Data has been saved!")
else
print("There was an error when saving data.")
wait(errormessage)
end
end
The reason GetNameFromUserIdAsync() is returning nil is because you have “-time” added on the end of it thats not a number try removing it and saving some data then run your script
Edit: Ive said that before i dont know if you made the change