So I’ve been trying to make a custom inventory system to advance my knowledge of Roblox scripting and data stores and I feel like I’m done with all the necessary code but when I run it either doesn’t save sometimes or sometimes the data I try to get always returns null.
For some reason, it keeps changing whenever I change something (I’m not sure what because I don’t see anything wrong with it) I might just be missing something though.
local DataStoreService = game:GetService("DataStoreService")
local InventoryDatastore = DataStoreService:GetDataStore("InvStore")
local UIS = game:GetService("UserInputService")
local pickupItemEvent = game.ReplicatedStorage.PickupItem
local saveData = game.ReplicatedStorage:WaitForChild("SaveData")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
--Adds player data folder
local playerData = Instance.new("Folder", game.ServerStorage.Inventory)
playerData.Name = player.Name
local success, err = pcall(function()
--Get the player's data store and clone the pickable partsR objects to the players inventory from their names
local invItemsSave = InventoryDatastore:GetAsync(player.Name)
print(invItemsSave)
if invItemsSave ~= nil then
for i, v in pairs(invItemsSave) do
print(">>>Debug: Got data from store")
local invItem = game.ReplicatedStorage.PickablePartsR:FindFirstChild(v)
if invItem then
invItem:Clone().Parent = game.ServerStorage.Inventory:WaitForChild(player.Name)
end
end
end
end)
if success then
print("Data successfully got")
else
print("Error getting data!")
warn(err)
end
end)
--When called, destroys the item and adds it to the players inventory folder
local function pickupItem(player, item)
--Add the item to the Player inventory folder
item:Destroy()
local invItem = item:Clone()
invItem.Parent = game.ServerStorage.Inventory:WaitForChild(player.Name)
--Fires a mudulescript that updates the UI
end
pickupItemEvent.OnServerEvent:Connect(pickupItem)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
--Create a table with all the current inventory object names
local invSave = {}
--Add that table to the local players data store: InventoryDataStore:SetAsync(Player.Name, "the table with all the inventory objects in text form")
for i, v in pairs(game.ServerStorage.Inventory:WaitForChild(player.Name):GetChildren()) do
if v then
table.insert(invSave, v.Name)
end
end
InventoryDatastore:SetAsync(player.Name, invSave)
print(">>>Debug: "..invSave)
end)
if success then
print("Data successfully saved")
else
print("Error saving data!")
warn(err)
end
end)
First problem: This is the one that happened first. Whenever I join the game for the first time it does nothing as (if invItemsSave ~= nil). So when I pick up some items, leave the game and join back the “invItemsSave” is still empty(so it never goes past if invItemsSave ~= nil) even though it showed that the saving was successful with the pcall() and even the invSave table in the player removing event had the names of the items that were picked up.
Second problem: I messed around with it trying to fix it but now with the code attached it does not even print “data successfully saved” in the player leave pcall. I think that this might be because the game might have already quit and it’s not printing (i think it had shown earlier though) and if that’s the case then I guess the first problem is the only one
I don’t know what I am doing wrong I’ve checked other posts with the same problem and they all have different solutions such as typing the wrong key in the GetAsync or something. Also if there’s anything else that could be neater/faster/optimized then lmk too!
Thank You, this is my first forum post so if something is formatted wrong ill try to fix it.