Hello. I am making an inventory, and I am looking to save a player’s inventory whenever he leaves under a table. Will that work:
local module = require(script.Inventory);
local inventoryDataStore = dataStoreService:GetDataStore("InventoryDataStore");
local function player_Removing(player)
local data = inventoryDataStore:GetAsync(player.UserId);
if data then
inventoryDataStore:UpdateAsync(inventoryDataStore, function() -- There was an already existing key, so I didn't create a new key rather updated the existing one.
return module:GetInventory(player)
end)
else
local newDataStore = inventoryDataStore:SetAsync(player.UserId, nil); -- Creating a new data store key.
inventoryDataStore:UpdateAsync(inventoryDataStore, function() -- Updating the existing data store key.
return module:GetInventory(player)
end)
end
end;
players.PlayerRemoving:Connect(player_Removing)