Datastore not saving, loading etc?

Data store isn’t working. It’s meant to save ores.

local datastore = game:GetService("DataStoreService")
local inventoryStore = datastore:GetDataStore("InventoryStore")

for _,v in pairs(game.ReplicatedStorage.Ores:GetChildren()) do
    if v:IsA("Part") and v.Name ~= "Hay" then
        local ore = Instance.new("NumberValue",script.Inventory)
        ore.Name = v.Name
        local Sellvalue = Instance.new("NumberValue",ore)
        Sellvalue.Value = v:WaitForChild("Sell").Value
        Sellvalue.Name = "SellValue"
    
        elseif v.Name == "Hay" then
            local Sell = Instance.new("NumberValue",v)
        Sell.Value = v:WaitForChild("Sell").Value
        Sell.Name = "SellValue"    
    
        
    end    

end


game.Players.PlayerAdded:Connect(function(plr)
    if plr then
        local inv = script.Inventory
        inv:Clone().Parent = plr
        for _,v in pairs(inv:GetChildren()) do
            print("Loading Data.")
            v.Value = inventoryStore:GetAsync(plr.UserId..v.Name)
            print("Loaded Data!",v.Value)
            
            v.Changed:Connect(function()
                    print("Saving Data...")
            wait(.3)
                    inventoryStore:SetAsync(plr.UserId..v.Name,v.Value)
                    print("Data Saved!")
                end)
            
            game.Players.PlayerRemoving:Connect(function()
                for _,v in pairs(inv:GetChildren()) do
                    print("Saving Data...")
                wait(.3)
                    inventoryStore:SetAsync(plr.UserId..v.Name,v.Value)
                    print("Data Saved!")
            end
            end)
        end
    end
end)

Try putting the PlayerRemoving function outside of the PlayerAdded function.

1 Like

The PlayerRemoving function should not be in the PlayerAdded function, as @2mzh said. Also, in case data saving / loading errors, you’ll want to use pcalls

1 Like

Alright. My data hasn’t saved still, aswell as loading.

  1. You should wrap any Async in a pcall function, because it’s possible to error at any given time.

  2. If you are testing this in studio, you should have a bindtoclose function.

  3. If you are saving the inventory every time something changes you will most likely reach your limits very quickly. If you want to save every change, you might want to look into datastore2.

Apart from those, does anything print?

The changed part,
just “Saving Data…”

For the saving when you leave,
just “Saving Data”

For the loading,
just “Loading Data…” “Done Loading!” except nothings been loaded.

Try something like this for the changed function.

v.Changed:Connect(function()
                    print("Saving Data...")
            wait(.3)
local success, err = pcall(function()
    inventoryStore:SetAsync(plr.UserId..v.Name,v.Value)
end)
if success then
    print("Data Saved!")
else
    warn(err)
end

And see if it prints anything.

Alright. (30 charssssssssssssssssss)

Nothing had printed. nor errored, so no data saved.