Argument 1 missing or nil? (OrderedDataStore error)

Hello everyone! I’ve recently been attempting to create a global leaderboard on a SurfaceGui that displays the top 50 players in the game according to their win count, via OrderedDataStores. This is what I have so far (inside a script in ServerScriptService):

local players = game:GetService('Players')

local replicatedStorage = game:GetService('ReplicatedStorage')

local DSS = game:GetService('DataStoreService')
local winsStore = DSS:GetOrderedDataStore('WinsStore')

local function UpdateLeaderboard() --Creates the leaderboard on an interval
	local success, errorMessage = pcall(function()
		local DataArray = winsStore:GetSortedAsync(false,50,0,1000000) --Sets which section of the DataStore entries I want to utilizie
		local winsPage = DataArray:GetCurrentPage() --Returns the data in an array
		for rank, data in ipairs(winsPage) do --The array holds a key and value (like a dictionary) for every entry (taken from the API reference for OrderedDataStore)
            print(data.Value) -- Prints nil
			local wins = data.Value
			local username = players:GetNameFromUserIdAsync(tonumber(data.Key))
			local isOnLeaderboard = false
			for i,frame in pairs(workspace.WinsLeaderboard.SurfaceGui.Main.Holder:GetChildren()) do
				if frame.PlayerLabel.Text == username then
					isOnLeaderboard = true
					break
				end
			end
			
			if wins and isOnLeaderboard == false then --Makes a new leaderboard frame for the player
				local playerFrame = replicatedStorage:WaitForChild('PlayerFrame'):Clone()
				playerFrame.PlayerLabel.Text = username
				playerFrame.Rank.Text = "#"..rank
				playerFrame.Wins.Text = wins
				playerFrame.Profile.Image = 'rbxassetid://'..players:GetUserThumbnailAsync(username.UserId)
				playerFrame.Position = UDim2.new(0,0,playerFrame.Position.Y.Scale + (0.025 * #workspace.WinsLeaderboard.SurfaceGui.Main.Holder:GetChildren()),0)
				playerFrame.Parent = workspace.WinsLeaderboard.SurfaceGui.Main.Holder
			end
		end
	end)
	
	if not success then
		print(errorMessage) --Prints "Argument 1 missing or nil"
	end
end

while true do --To autosave and to update the leaderboard
	for _, frame in pairs(workspace.WinsLeaderboard.SurfaceGui.Main.Holder:GetChildren()) do --Destroys the current Leaderboard
		frame:Destroy()
	end
	for _, player in pairs(players:GetPlayers()) do --Saves the wins of each player
		winsStore:SetAsync(player.UserId, player.leaderstats.Wins.Value)
	end
	UpdateLeaderboard() --Calls the function that updates the leaderboard
	wait(60)
end

These are the errors I’ve been experiencing:

  1. On line 37, print(err) outputs the message “Argument 1 missing or nil”

  2. print(data.Value) outputs nil

I’ve rechecked everything to make sure it coincided with different tutorials on youtube and that all the syntax was correct via the API reference page, and everything seems to be in order.

In a separate script, I use GetAsync() to return the value of Wins and it works without any problem (meaning that data entries exist, and therefore shouldn’t return nil). Both scripts use the same OrderedDataStore.

Other than this, the function seems to stop running when it tries to get the username, but nonetheless since print(data.Value) prints nil I’m guessing that the problem lies in GetSortedAsync() or GetCurrentPage(), but after trying everything I can’t get it to work.

Thank you very much for your patience and your time!

2 Likes

Hey @Lomstair!

I’m really sorry I can’t help more than this right now, but are you certain there are values in that datastore to show?

If there aren’t any values then that would explain what’s going on. Possible check the datastore, and if there are none, check out why in whatever script handles setting those values.

Hope this helped!

Get rid of the pcall for a second to see exactly what line is having a problem.

1 Like

Hello @RepValor, thank you very much for responding! I’m just glad you’re helping me, no rush!

You’ve made a great point and honestly I should’ve included this excerpt in the post, but this is the other script which sets up the player (only displaying the lines related to loading and saving wins).

It outputs no errors and works as intended. The Key used for the OrderedDataStore is the same as in the leaderboard one (player.UserId).

I’ve verified that it loads the wins value without any problems when a player joins and also saves it when a player leaves.

--Variables
local players = game:GetService('Players')

local DSS = game:GetService('DataStoreService')
local winsStore = DSS:GetOrderedDataStore('WinsStore') --Same OrderedDataStore as in the other script

--Functions
players.PlayerAdded:Connect(function(player) --Fires when a player joins
	local leaderstats = Instance.new('Folder') --The leaderstats folder, used to display the amount of wins
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local wins = Instance.new('IntValue') --The int value that holds the value of wins
	wins.Name = "Wins"
	wins.Parent = leaderstats
	
	local myWins = winsStore:GetAsync(player.UserId) --Returns the datastore entry value
	
	if myWins then
		wins.Value = myWins --Works perfectly, returns the correct value every time any player joins
	else
		winsStore:SetAsync(player.UserId, wins.Value) --Sets the value to 0 in case of a new player
	end	
end)

players.PlayerRemoving:Connect(function(player)
	winsStore:SetAsync(player.UserId, player.leaderstats.Wins.Value) --Saves the number of wins when leaving
end)

Thank you for your assistance!

1 Like

Hi @JarodOfOrbiter. I just tried what you asked and the error lies on this line:

local username = players:GetNameFromUserIdAsync(tonumber(data.Key))

The error is:

Argument 1 missing or nil
01:16:57.816 - Stack Begin
01:16:57.818 - Script 'ServerScriptService.WinsLeaderboard', Line 15 - function UpdateLeaderboard
01:16:57.820 - Script 'ServerScriptService.WinsLeaderboard', Line 47
01:16:57.821 - Stack End

As far as I understand this means that neither a Key nor a Value exist for each data entry, since print(data.Value) prints nil. This is extremely puzzling since I can use GetAsync() no problem to receive the value from the same OrderedDataStore in my player setup script.

Thank you very much for the help!

Alright so it would seem that your only issue here is that data is nil.

That would explain the error.

So for some reason your loop is not working as intended, or maybe the :GetSortedAsync() isn’t working, or maybe the :GetCurrentPage() is what isn’t working. Honestly I’m not really certain.

I would look up a tutorial on leaderboards like this and try to see the differences between your and the tutorials.

It looks like data.Key isn’t a number.

Hey @JarodOfOrbiter:

data is a dictionary in the sense that it holds a Key (player.UserId) and Value (wins). When using DataStores the Key usually contains the UserId of the player in some way, and doesn’t have to be a number. At least I believe it is that way!

Thanks for these suggestions! I’ll continue to compare my script to the API reference syntax and any more tutorials I can find, and if not, I guess I’ll have to find alternative ways.

I agree with your suspicions of either :GetSortedAsync() or :GetCurrentPage(), as I don’t use them when receiving data from the OrderedDataStore in the PlayerSetup script, so it’s likely that they might be causing the problem.

It does need to be able to be converted to a number though. Just print what it is, because tonumber returns nil when it can’t convert, which would cause this error

print(data.Key) prints nil… Very strange indeed!

Hmm, is there a possibility that data is equal to 0. Maybe add a if statement to check if it’s greater then 0, before proceeding to this line: local username = players:GetNameFromUserIdAsync(tonumber(data.Key))

After attempting this, I got the following ErrorMessage:

  ServerScriptService.WinsLeaderboard:14: attempt to compare number and nil

Data just isn’t in the mood to contain any values I guess…

Pretty sure it’s data.key, not data.Key. Lowercase k.

1 Like

That was exactly the problem! I made a loop to iterate thorugh all the data entry values and before I read your message I got 9 nils, now I’m getting 9 values instead! Thank you very much for your help, I would’ve never figured this out otherwise!

1 Like