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

Why does this DataStore save script not work?

Asked by 8 years ago

I am making an Inventory system that loads and saves using a DataStore. When I test it in Play Solo, the game crashes and closes Edit mode (really annoying). When I test a server with two players and have one leave, I get a very unclear error that says 10:19:36.987 - An error occurred 10:19:36.987 - Script 'http://www.roblox.com/game/gameserver.ashx', Line 118 10:19:36.988 - Stack End

I added a print function into the code to print the values to see if they were actually being recognized. They were, but it printed each new inventory slot at different time increments, far away from each other. So why is my data saving so slowly? Is there a better way to save an inventory of items that I should be using? (All the data is strings, I know you can't save Instances)

Here is the PlayerRemoving code:

game.Players.PlayerRemoving:connect(function(Player)
    local Key = Player.userId
    local Inventory = Player:FindFirstChild('Inventory')
    if Inventory then
        for i=1, 20 do
            local Slot = Inventory:FindFirstChild('Slot'..i)
            if Slot then
                print(Slot.Value..'[Printed at'..game.Workspace.DistributedGameTime)
                InventoryStore:SetAsync(Key, Slot.Value)
            end
        end
    end
end)

and here is what the output is reading with the Game Times to show you how spaced out the saving is:

Player -1 leaving 10:32:47.579 - An error occurred 10:32:47.580 - Script 'http://www.roblox.com/game/gameserver.ashx', Line 118 10:32:47.580 - Stack End TestObject2[Printed at31.008334964048 TestObject3[Printed at37.816668654326 TestObject4[Printed at44.595835674554 TestObject5[Printed at51.279169356916

Answer this question