Attempt to compare number with nil (OrderedDataStore)

I have a problem with a script I’m doing for a top leaderboard, the Output tells me:
“attempt to compare number with nil”, How can I solve that ?

Why is “v” nil ?

Script:


local SurfaceGui = script.Parent
local Holder = script.Parent.Frame.ScrollingFrame

function Main ()
	local Success, Error = pcall(function()
		local Pages = OrderedDataStore:GetSortedAsync(false, 10)
		local Data = Pages:GetCurrentPage()
		
		for k, v in pairs(Data)do
			if tonumber(v.key) >= 1 then
				local PlayerName = game.Players:GetNameFromUserIdAsync(v.Key)
				local Level = tostring(v.Value)
				print(PlayerName, Level)
				local NewFrame = script.Frame:Clone()
				
			    NewFrame.PlayerPos.Text = k..". "..PlayerName
			    NewFrame.PlayerCurrency = "Nivel "..Level
			    NewFrame.Parent = Holder
		   end
		end
	end)
	if not Success then
		error(Error)
	end
end
while true do
	for _, Player in pairs(game.Players:GetChildren())do
		local PlayerValue = Player.leaderstats.Nivel.Value
		OrderedDataStore:SetAsync(Player.UserId, PlayerValue)
	end
	Main()
	wait(10)
end```

So you’re comparing something that is actually nil with a number. The only place I see a comparison is at the line

if tonumber(v.key) >= 1 then

So, tonumber(v.key) is actually nil and you’re trying to compare it with the number 1. Are you sure v.key is what you want? In this loop the value k is the key for the dictionary. Try tonumber(k) instead!

1 Like

If you do this:
```lua
your code goes here
```
then your code will be formatted properly on the forum. It’s hard to read right now, making it hard to help you.

2 Likes

the k is the position. I’m not very familiar with OrderedDataStore but I saw tutorials that use these techniques and put “v.key” that would be from GetCurrentPage () to get the stored (I guess)