I have a folder named Inventory which holds BoolValues of item names such as Rocks,Stones,Gold,etc and I want the datastore to save the folder and its contents. Its not saving or displaying any errors.
Current datastore code:
local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData')
wait(5)
game.Players.ChildAdded:Connect(function(player)
local data = dss:GetAsync(player.UserId) or {}
local inventory = player.Inventory
for _,v in pairs(data) do
local value = inventory:GetChildren()
value.Parent = inventory
end
end)
game.Players.ChildRemoved:Connect(function(player)
local tbl = {}
local success, message = pcall(function()
for _,v in pairs(player.Inventory:GetChildren()) do
tbl[#tbl+1] = v.Name
end
dss:SetAsync(player.UserId, tbl)
end)
if success then
print("Saved "..player.Name.."'s data")
end
end)
local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData')
game.Players.PlayerAdded:Connect(function(player)
local Success, Error
Success, Error = pcall(function()
data = dss:GetAsync(player.UserId)
end)
local inventory = player:WaitForChild("Inventory")
if Success then
if data ~= nil then
for i,v in pairs(data) do
local Item = inventory:FindFirstChild(i)
Item.Value = v
end
end
else
print(Error)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local tbl = {}
for i, v in pairs(player.Inventory:GetChildren()) do
table.insert(tbl, v.Name, v.Value)
end
local success, message = pcall(function()
dss:SetAsync(player.UserId, tbl)
end)
if success then
print("Saved "..player.Name.."'s data")
else
print(message)
end
end)
This means that the item that it’s try to find is not in the player’s inventory folder. Try this script to see what is being saved and what is being loaded. Also. you should use the output to see the tables printed.
local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData')
game.Players.PlayerAdded:Connect(function(player)
local Success, Error
Success, Error = pcall(function()
data = dss:GetAsync(player.UserId)
end)
local inventory = player:WaitForChild("Inventory")
if Success then
if data ~= nil then
print(data)
for i,v in pairs(data) do
local Item = inventory:FindFirstChild(i)
Item.Value = v
end
end
else
print(Error)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local tbl = {}
for i, v in pairs(player.Inventory:GetChildren()) do
table.insert(tbl, v.Name, v.Value)
end
print(tbl)
local success, message = pcall(function()
dss:SetAsync(player.UserId, tbl)
end)
if success then
print("Saved "..player.Name.."'s data")
else
print(message)
end
end)
for i, v in pairs(player.Inventory:GetChildren()) do -- This line gets all the inventory's children
table.insert(tbl, v.Name, v.Value) -- This line puts them into the table along with their values
end
So you’re already creating the BoolValues in the different script when the player joins the game. Ok, didn’t realize that.
What Neon is trying to do is help you save the value of the item to the table so it corresponds with the name of the BoolValue.
However, this would not work.
table.insert(tbl, v.Name, v.Value)
table.insert takes in a number as a second argument, not a key.
Initially you only saved the names of the BoolValues to a table so that’s why I got confused. The only reason someone would do that is if they were only saving a table of strings. There is both an issue with how you save your data and load your data.
local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData')
game.Players.PlayerAdded:Connect(function(player)
local Success, Error
Success, Error = pcall(function()
data = dss:GetAsync(player.UserId)
end)
local inventory = player:WaitForChild("Inventory")
if Success then
if data ~= nil then
print(data)
for i,v in pairs(data) do
local Item = inventory:FindFirstChild(i)
Item.Value = v
end
end
else
print(Error)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local tbl = {}
for i, v in pairs(player.Inventory:GetChildren()) do
tbl[v.Name] = v.Value
end
print(tbl)
local success, message = pcall(function()
dss:SetAsync(player.UserId, tbl)
end)
if success then
print("Saved "..player.Name.."'s data")
else
print(message)
end
end)