Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to compare number with nil (OrderedDataStore)?

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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.

Ad

Answer this question