Hello! So I want to make an Inventory Data Store. It prints the data is saved but when i re-join the values aren’t there!
local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(player)
local Inventory = Instance.new("Folder", player)
Inventory.Name = "Inventory"
local key = "pi_"..player.UserId
local data
local success, errorMessage = pcall(function()
data = InventoryDataStore:GetAsync(key)
end)
if success then
if data then
for i, v in pairs(Inventory:GetChildren()) do
v.Value = data.v
end
print("✅ | Loaded Inventory")
else
print("❎ | Could not load Inventory "..tostring(errorMessage))
end
end
end)
local function saveInv(player)
local key = "pi_"..player.UserId
local data = {}
for i, v in pairs(player.Inventory:GetChildren()) do
table.insert(data, i, v)
end
local success, errorMessage = pcall(function()
InventoryDataStore:SetAsync(key, game:GetService("HttpService"):JSONEncode(data))
end)
if success then
print("✅ | Saved Inventory")
else
print("❎ | Could not save Inventory "..tostring(errorMessage))
end
end
game.Players.PlayerRemoving:Connect(saveInv)
game:BindToClose(function(player)
if RunService:IsStudio() then
return
end
saveInv(player)
end)
Hmm… I would probably replace the dataStore code with this because I found many errors in the original one:
local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")
game.Players.PlayerAdded:Connect(function(player)
local Inventory = Instance.new("Folder", player)
Inventory.Name = "Inventory"
local data
pcall(function()
data = InventoryDataStore:GetAsync("pi_" .. player.UserId)
end)
if data then
for i, v in pairs(Inventory:GetChildren()) do
v.Value = data
end
print("✅ | Loaded Inventory")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {}
for i, v in pairs(player.Inventory:GetChildren()) do
table.insert(data, v) -- Here, you put data, i, v instead of data, v. This meant that your get function couldn't read the data!
end
pcall(function()
InventoryDataStore:SetAsync("pi_" .. player.UserId, data)
end)
end)
That’s why it isn’t working. You are asking the code to loop through the children of the Inventory folder when the Inventory folder does not have any children.
local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")
game.Players.PlayerAdded:Connect(function(player)
local Inventory = Instance.new("Folder", player)
Inventory.Name = "Inventory"
local StringVal = Instance.new("StringValue", Inventory) -- Added string
StringVal.Name = "String"
local data
pcall(function()
data = InventoryDataStore:GetAsync("pi_" .. player.UserId)
end)
if data then
for i, v in pairs(Inventory:GetChildren()) do
v.Value = data
end
print("✅ | Loaded Inventory")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {}
for i, v in pairs(player.Inventory:GetChildren()) do
table.insert(data, v) -- Here, you put data, i, v instead of data, v. This meant that your get function couldn't read the data!
end
pcall(function()
InventoryDataStore:SetAsync("pi_" .. player.UserId, data)
end)
end)
Sorry to intervene. But I believe using the folder itself is apart of the problem because to use the folder you would need to store multiple instances ( instances being stringvalues, number values, etc.).
I recommend you just use the inventory data within table form. And to edit and view the table I also recommend making a module for managing the inventory data.
The “Inventory” folder is empty, that is why it does not load the data, there is nothing inside the folder.
I tried to fix your code quickly, hope it works:
local DataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(player)
local Inventory = Instance.new("Folder", player)
Inventory.Name = "Inventory"
local key = "pi_"..player.UserId
local data
local success, errorMessage = pcall(function()
data = InventoryDataStore:GetAsync(key)
end)
if success then
if data then
for i,v in pairs(data) do
local Add = Instance.new("StringValue")
Add.Value = v
Add.Parent = Inventory
end
print("✅ | Loaded Inventory")
else
print("❎ | Could not load Inventory "..tostring(errorMessage))
end
end
end)
local function saveInv(player)
local key = "pi_"..player.UserId
local data = {}
for i, v in pairs(player.Inventory:GetChildren()) do
table.insert(data, i, v)
end
local success, errorMessage = pcall(function()
InventoryDataStore:SetAsync(key, game:GetService("HttpService"):JSONEncode(data))
end)
if success then
print("✅ | Saved Inventory")
else
print("❎ | Could not save Inventory "..tostring(errorMessage))
end
end
game.Players.PlayerRemoving:Connect(saveInv)
game:BindToClose(function(player)
if RunService:IsStudio() then
return
end
saveInv(player)
end)