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

Inventory Save (ERROR? No output messages!)

Asked by 5 years ago

I have this inventory save script that should work and I think its saving the data but its not loading the data can you tell me what i'm doing wrong?

Here's the script!

local inventoryData = game:GetService("DataStoreService"):GetDataStore("inventoryTest")
local inventoryHolder = script.InventoryHolder

game.Players.PlayerAdded:Connect(function(player)
    local Key = "Player-ID"..player.UserId
    local GetSave = inventoryData:GetAsync(Key)

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

    for i, inventory in pairs(inventoryHolder:GetChildren()) do
        local newInventory =  Instance.new(inventory.ClassName)
        newInventory.Name = inventory.Name
        newInventory.Value = inventory.Value
        newInventory.Parent = DataBaseFolder
    end

    if GetSave then
        for i,v in pairs(GetSave) do
            local testValue = game.ServerStorage.TrailValues:FindFirstChild(v)
            for i = 1, #testValue do 
                if testValue then
                    local clone = testValue()
                    clone.Parent = DataBaseFolder
                    clone.Name = v
                end
            end
        end 
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Key = "Player-ID"..player.UserId
    local DataBaseFolder = player.Inventory
    local InventoryToSave = {}

    for i,v in pairs(DataBaseFolder:GetChildren()) do
        table.insert(DataBaseFolder:GetChildren(), v.Name)
    end
    inventoryData:SetAsync(Key, InventoryToSave)
end)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

table.insert(DataBaseFolder:GetChildren(), v.Name) is wrong. If you insert your value in DataBaseFolder your table InventoryToSave rest empty! So you save a empty table..

local HttpService = game:GetService('HttpService')

local inventoryData = game:GetService("DataStoreService"):GetDataStore("inventoryTest")
local inventoryHolder = script.InventoryHolder

game.Players.PlayerAdded:Connect(function(player)
    local Key = "Player-ID"..player.UserId
    local GetSave = inventoryData:GetAsync(Key)

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

    for i, inventory in pairs(inventoryHolder:GetChildren()) do
        local newInventory =  Instance.new(inventory.ClassName)
        newInventory.Name = inventory.Name
        newInventory.Value = inventory.Value
        newInventory.Parent = DataBaseFolder
    end

    if GetSave ~= nil and type(GetSave) == 'string' then
        print('JSON detected!')
        pcall(function()
            local decode = HttpService:JSONDecode(GetSave)
            if type(decode) == 'table' then
                print('Decode successfull!')
                for i, v in pairs(decode) do
                    local testValue = game.ServerStorage.TrailValues:FindFirstChild(v)
                    for i = 1, #testValue do 
                        if testValue then
                            local clone = testValue:Clone()
                            clone.Parent = DataBaseFolder
                            clone.Name = v
                        end
                    end
                end
            end
        end)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Key = "Player-ID"..player.UserId
    local DataBaseFolder = player:WaitForChild('Inventory')
    local InventoryToSave = {}

    for i, v in pairs(DataBaseFolder:GetChildren()) do
        table.insert(InventoryToSave, v.Name)
    end

    -- You cannot save string and bool with DataStoreService! Use HttpService:JSONEncode() to save your data
    inventoryData:SetAsync(Key, HttpService:JSONEncode(InventoryToSave))
end)

Theoretically it should work

Ad

Answer this question