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

Inventory isn't being saved by table, only prints to the first line?

Asked by 3 years ago
game.Players.PlayerRemoving:Connect(function(plr)
    print("running removing")
    local priorData = dataStore:GetAsync(plr.UserId)
    print('loaded old data')
    local dataToSave = {
        ['gagueStats'] = {
            ['gglvl'] = plr.gg.Value,
            ['ggxp'] = plr.ggEXP.Value,
            ['tp'] = plr.tp.Value, 
            ['str'] = plr.str.Value, 
            ['strxp'] = plr.strEXP.Value,
            ['int'] = plr.int.Value, 
            ['intxp'] = plr.intEXP.Value,
            ['end'] = plr['end'].Value,
            ['ednxp'] = plr.endEXP.Value,
            ['dex'] = plr.dex.Value,
            ['dexxp'] = plr.dexEXP.Value    
        },
        ['appearanceStats'] = priorData['appearanceStats'],
        ['spiderwebCompletion'] = priorData['spiderwebCompletion'],
        ['inventory'] = {
        },
        ['gold'] = plr.gold.Value
    }
    print('table created')
    for i,v in pairs(plr.inv:GetChildren()) do
        print(v)
        table.insert(dataToSave['inventory'], i, v.Name)
    end
    print('inv stuff added')
    dataStore:SetAsync(plr.UserId, dataToSave)
    print('saved')
end)

The goal is to take everything inside a folder named "inv" inside the player and save it under the inventory table in the data table, however, when I load back into the game there are no items in my inventory except for one copy of trash when there should be two (I have a script to add it to my inventory every time I load in)

When I tried print debugging, it only printed down to "running removing" which is the first print. Why would nothing after this run? There are no errors for it either.

0
I've also tried testing this outside of studio, and it still didn't work itchymoonfire 179 — 3y
0
Update, it says attempted to index nil with GetAsync ingame when my friend left. Any ideas? itchymoonfire 179 — 3y
0
Try removing the i, part after ['inventory;] and before v CandyWreckEpicness 115 — 3y
0
Didn't seem to work itchymoonfire 179 — 3y
0
You haven't declared dataStore, add a line at the top of the code saying "local dataStore = game:GetService("DataStoreService"). I'm sorry if this didn't help, haven't scripted in months :D CoinWield 47 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

You could use a separate script for the inventory i suggest you creating a folder with the items in replicated storage and storing only the items name, so you can clone them and put it on player's inventory

local datastorage = game:GetService("DataStorage") -- get the service

local inventoryStorage = datastorage:GetDataStore("inventory") -- get the data store

game.Players.PlayerAdded:connect(function(plr)

local inv_save = inventoryStorage:GetAsync(plr.UserId) or {""}
-- anything you want to do with the inventory here, i don't really know how your system works

end)

game.Players.PlayerRemoving:connect(function(plr)
     local items = plr.inv:GetChildren()
local stored = {}

for _,v in pairs(items) do
    table.insert(stored,#stored + 1,v.Name)
end

     inventoryStorage:SetAsync(plr.UserId,stored)

local functiontest1,functiontest2 = pcall(function() -- just to be sure it saved.
     inventoryStorage:UpdateAsync(plr.UserId,stored)
end)

end)
Ad

Answer this question