Hello! I need to make an Inventory Data Store for my game. It prints that the data is saved but when I join all the values are 0. How can I fix it?
local playerService = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local InventoryDataStore = dataStoreService:GetDataStore("InventoryDataStore")
local playerData = require(game.ServerStorage.PlayerData)
local DataHandler = {}
local function getData(player)
local Inventory = Instance.new("Folder", player)
Inventory.Name = "Inventory"
for i, plant in ipairs(game.ReplicatedStorage.Plants:GetChildren()) do
local int = Instance.new("IntValue", Inventory)
int.Name = plant.Name
end
local playerUserId = player.UserId
local data
local success = pcall(function()
data = InventoryDataStore:GetAsync(playerUserId)
end)
if success then
if data then
for i, v in ipairs(Inventory:GetChildren()) do
v.Value = data[v]
end
else
warn("Error whilst getting data")
end
end
return success
end
local function saveData(player)
local playerUserId = player.UserId
for i, plant in ipairs(player.Inventory:GetChildren()) do
table.insert(playerData, i, plant.Value)
end
print(playerData)
local success = pcall(function()
InventoryDataStore:SetAsync(playerUserId, playerData)
end)
return success
end
playerService.PlayerAdded:Connect(getData)
playerService.PlayerRemoving:Connect(saveData)
return DataHandler
You forgot to set the values.
And you’re not saving the plant Name
for i, plant in ipairs(game.ReplicatedStorage.Plants:GetChildren()) do
local int = Instance.new("IntValue", Inventory)
int.Name = plant.Name --< You forgot to set the value of plant
end
for i, plant in ipairs(player.Inventory:GetChildren()) do
table.insert(playerData, i, plant.Value) --< You forgot to save the plant.Name
end
1 Like
Thank you for replying! How can I save the plant name?
Just add it to table
for i, plant in ipairs(player.Inventory:GetChildren()) do
table.insert(playerData, i, plant.Value, plant.Name)
end
1 Like
Now I get this error:
wrong number of arguments to 'insert'
Oh my bad sorry, sometimes i am dumb.
You need to add a table in a table
for i, plant in ipairs(player.Inventory:GetChildren()) do
table.insert(playerData, i,{Value = plant.Value; Name = plant.Name})
end
Hmm… It still says that the value is 0.
Have you set the values of plant?
if success then
if data then
for i, v in ipairs(Inventory:GetChildren()) do
v.Value = data[v]
end
else
warn("Error whilst getting data")
end
end
Try it
if success then
if data then
for i, v in pairs(data) do
Inventory:GetChildren()[data[i].Name].Value = data[i].Value
end
else
warn("Error whilst getting data")
end
end
bruh it still says that its 0 and this time it errors:
attempt to index nil with 'Value'
… I removed :GetChildren()
if success then
if data then
for i, v in pairs(data) do
Inventory[data[i].Name].Value = data[i].Value
end
else
warn("Error whilst getting data")
end
end
IT WORKS!!! Thank you so much!!!
You are welcome. Don’t forget the solution tag. 
1 Like