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

Inventory datastore not working despite no errors?

Asked by 2 years ago

I was writing an inventory datastore script that saves the items in the player's inventory and loads in when they join in the game next time. However, when I leave the game and join back, the weapons in my inventory I had previously don't load in my inventory now. No errors appeared on the output and there were no error messages in the pcall functions.

I put some weapons in a folder named Weapons which were in ServerStorage so I can see if the player has any weapons.

Script (A regular script in ServerScriptService):

01> local DSS = game:GetService("DataStoreService")
02> local InventoryDS = DSS:GetDataStore("InventoryDS")
03> local Players = game:GetService("Players")
04>
05> local function onPlayerAdded(plr)
06>   local key = "User_"..plr.UserId
07>   local inventoryReference = game.ServerStorage.Weapons
08>  
09>   local inventoryData
10>   local success, errorMessage = pcall(function()
11>       inventoryData = InventoryDS:GetAsync(key)
12>      
13>       if inventoryData then
14>           print("check")
15>           for i, weaponName in pairs(inventoryData) do
View all 57 lines...

As always, thank you for taking the time to read the script and I will appreciate any help I receive.

0
Is enable api services turned on theking66hayday 841 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

Looks like you're saving your inventory before actually adding any weapons to the table that's being saved.

Consider changing the order of:

1InventoryDS:SetAsync(key,playerInventoryTable)
2 
3for i,weapon in pairs(playerInventory) do
4    print("2")
5    table.insert(playerInventoryTable, weapon.Name)
6end

to:

1for i,weapon in pairs(playerInventory) do
2    print("2")
3    table.insert(playerInventoryTable, weapon.Name)
4end
5 
6InventoryDS:SetAsync(key,playerInventoryTable)
Ad

Answer this question