Hello!
I’ve been having an issue with saving tables when there are multiple values in the table that are equal to each other. For example, if we have a table of {“a”,“b”,“b”,“c”}, only {“a”,“b”,“c”} get saved to Datastore2. I’m new to datastores in general so sorry if I made a common mistake.
Here’s my script:
local Datastore = require(game.ServerScriptService.DataStore2)
local IDs = require (game.ServerStorage.Items["!IDs"])
local players = game.Players
Datastore.Combine("DATA", "inventoryPR")
players.PlayerAdded:Connect(function(player)
local inventoryDatastore = Datastore("inventoryPR",player)
local invTempleate = game.ServerStorage:WaitForChild("Inventory"):Clone()
invTempleate.Parent = player
local invTable = inventoryDatastore:GetTable({})
print(invTable)
for i,itemSTR in pairs(invTable) do -- Inserts saved items into inventory
local item = IDs[itemSTR][1]
item.Parent = player:WaitForChild("Inventory")[item.Type.Value]
end
end)
players.PlayerRemoving:Connect(function(player)
local inventoryDatastore = Datastore("inventoryPR",player)
local playerInv = player:WaitForChild("Inventory")
local invSlots = {playerInv:WaitForChild("Food"),playerInv:WaitForChild("Materials"),playerInv:WaitForChild("Tools")}
local invToSave = {}
for i,slot in pairs(invSlots) do -- Gets a table to save later
for i,item in pairs(slot:GetChildren()) do
if item:FindFirstChild("StringID") then
table.insert(invToSave,item.StringID.Value)
end
end
end
print(invToSave)
inventoryDatastore:Set(invToSave)
end)