Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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 ?

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

Why is "v" null?

1 answer

If you've ever stored a key that isn't a number (or if v.key returns nil while v.Key does not), tonumber(v.key) would be nil, causing the error. Try printing out the value of v.key (and/or v.Key) to see what's going on.

This discussion is preserved as it was. New answers, comments, and votes are closed.