So as the title says I need some help with saving tables. Those tables contain Names and values the name represents the Name of the IntValue and value well it represents the value.
I tried to make a script which would save it but it doesn’t works and I don’t know why. This is the current script:
local dss = game:GetService("DataStoreService")
local InventoryData = dss:GetDataStore("InventoryDataStore")
local currentItems = game:GetService("ServerStorage").Items:GetChildren()
game.Players.PlayerAdded:Connect(function(player)
local key = "player_Inv" .. player.UserId
local inventory = InventoryData:GetAsync(key)
local folder = Instance.new("Folder", game:GetService("ServerStorage"):WaitForChild("Inventory"))
folder.Name = player.Name
for _, data in ipairs(inventory or { }) do
print("loop works")
local amount = player.Character:FindFirstChild(data).Value
print("can get the amount")
local Ival = Instance.new("IntValue", player.Character)
Ival.Value = amount
Ival.Name = data
print("can get the value")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "player_Inv" .. player.UserId
local tools = { }
local saveInventory = player.Character
for _, item in next, saveInventory do
if item.Name ~= currentItems.Name then
print("No item is in the player's inventory")
else
table.insert(tools, {
Name = item.Name,
Amount = item.Value
})
end
print("table has been inserted")
end
InventoryData:UpdateAsync(key, function(prev)
print("Inventory has been saved")
return tools
end)
end)
I am on my phone so I can’t do a good job, but I will try. Could you please send a screenshot of the output also?
local http = game:GetService(“HTTPservice”) —might have spelled this wrong
—encode data
Data = http:JSONEncode(Data)
—decode data
Data = http:JSONDecode(Data)
Sorry for any caps or spelling errors, am on phone. Make sure to replace “Data” with what you want to encode/ decode
local dss = game:GetService("DataStoreService")
local InventoryData = dss:GetDataStore("InventoryDataStore")
local http = game:GetService("HttpService")
local currentItems = game:GetService("ServerStorage").Items
game.Players.PlayerAdded:Connect(function(player)
local key = "player_Inv" .. player.UserId
local inventory = http:JSONDecode(key)
local folder = Instance.new("Folder", player)
folder.Name = player.Name .. " Obt"
for i, v in pairs(currentItems:GetChildren()) do
local Int = Instance.new("IntValue", folder)
Int.Name = v.Name
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "player_Inv" .. player.UserId
local InventoryIt = { }
local saveInventory = player:WaitForChild(player.Name .. " Obt")
for _, item in next, saveInventory do
table.insert(InventoryIt, {
Name = item.Name,
Amount = item.Value
})
end
InventoryData:UpdateAsync(key, function(prev)
http:JSONEncode(InventoryIt)
end)
end)
This is my current script its not finished can you give me feedback?
Sorry for the late response, as I am away I can’t get the example of json encoding and decoding.
You don’t need the key to encode or decode json, you just need the table data. So instead of encoding the key, encode the data. Also encode should be directly before you store it, and decode should be directly after you get the data
local dss = game:GetService("DataStoreService")
local InventoryData = dss:GetDataStore("InventoryDataStore")
local http = game:GetService("HttpService")
local currentItems = game:GetService("ServerStorage").ItemsValue
game.Players.PlayerAdded:Connect(function(player)
local data = {}
local folder = Instance.new("Folder", player)
folder.Name = player.Name .. "Obt"
if data then
data = http:JSONDecode(data)
if data["Coal Ore"] ~= nil then
if data["Coal Ore"] > 0 then
local id400 = Instance.new("IntValue", folder)
id400.Name = "Coal Ore"
id400.Value = data["Coal Ore"]
end
end
if data["Iron Ore"] ~= nil then
if data["Iron Ore"] > 0 then
local id402 = Instance.new("IntValue", folder)
id402.Name = "Iron Ore"
id402.Value = data["Iron Ore"]
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local inv = {}
for i, v in pairs(player:FindFirstChild(player.Name .. "Obt"):GetChildren()) do
if v.Value then
inv[v.Name] = v.Value
end
end
pcall(function()
InventoryData:SetAsync(player.UserId, http:JSONEncode(inv))
end)
end)
This is my current script now but it doesn’t works can you help me with this?