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

Sub-Table data doesn't load, invalid arguement #1 to 'ipairs'?

Asked by 2 years ago

Ok so, I'm making a mining simulator, and I want to make a tool DataStore. For some reason, the data won't load properly.

Script I used:

local DataStoreService = game:GetService("DataStoreService")
local DS = DataStoreService:GetDataStore("MinerzData")

local function loadData(player)
    local key = player.UserId .. "-minerzData"
    local data

    local success,errorMessage = pcall(function()
        data = DS:GetAsync(key)
    end)

    if success then
        print('MinerzData | Successfully loaded ' .. player.Name .. "'s data!")
        return data
    elseif errorMessage then
        warn('MinerzData | ' .. errorMessage)
        return nil
    end
end

local function saveData(player)
    local key = player.UserId .. "-minerzData"
    local data = {
        player.Items.Wood.Value,
        player.Items.Stone.Value,
        player.leaderstats.Cash.Value,
        pickaxes = {

        }
    }

    for i, pickaxe in pairs(player.Backpack:GetChildren()) do
        if pickaxe:IsA("Tool") then
            table.insert(data.pickaxes, pickaxe.Name)
        end
    end



    local success,errorMessage = pcall(function()
        DS:SetAsync(key, data)
    end)

    if success then
        print('MinerzData | Successfully saved ' .. player.Name .. "'s data!")
        return
    elseif errorMessage then
        warn('MinerzData | ' .. errorMessage)
        return
    end
end

game.Players.PlayerAdded:Connect(function(player)
    local playerItems = Instance.new("Folder", player)
    playerItems.Name = "Items"

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local cash = Instance.new("IntValue", leaderstats)
    cash.Name = "Cash"
    cash.Value = 0

    for i, item in pairs(game:GetService("ServerStorage"):WaitForChild("Items"):GetChildren()) do
        local newVal = Instance.new("IntValue", playerItems)
        newVal.Name = item.Name
        newVal.Value = 0
    end

    local data = loadData(player)

    if data == nil then
        warn(player.Name .. " must be a new player; data not found!")
    elseif data then
        playerItems:WaitForChild("Wood").Value = data[1]
        playerItems:WaitForChild("Stone").Value = data[2]
        cash.Value = data[3]
        for i, pickaxe in ipairs(data.pickaxes) do
            local exists = game:GetService("ReplicatedStorage"):WaitForChild("Pickaxes"):FindFirstChild(pickaxe)
            if exists then
                local newPick = exists:Clone()
                local newPick2 = exists:Clone()
                newPick.Parent = player:WaitForChild("Backpack")
                newPick2.Parent = player:WaitForChild("StarterGear")
            end
        end
    end

    player.CharacterRemoving:Connect(function(char)
        char:WaitForChild("Humanoid"):UnequipTools()
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    saveData(player)
end)



game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do
        saveData(player)
    end
end)

On line 78, it errors with:

ServerScriptService.InventoryHandler:79: invalid argument #1 to 'ipairs' (table expected, got nil)

On line 23 I define the "data" table in the saveData() function. I also define "pickaxes" as a sub-table. (line 27) For some reason, when I join the game, it says table expected, got nil. I don't know why this happens, and when I do a print(data.pickaxes), it just prints "nil."

If someone could help, it would mean a lot to me!

Answer this question