I have been trying to make my datastores save quicker but it takes time to save it. I need an efficient way to save and load a lot of datastores. But here is my code
function Save(player)
if player:WaitForChild("Inventory"):GetAttribute("Done") == true then
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Tools12")
local statstorage = player:FindFirstChild("Inventory"):GetChildren()
local itemKeys = {}
for i=1, #statstorage do
if statstorage[i] ~= nil or statstorage[i].ClassName ~= "ObjectValue" then
local cancel = false
if statstorage[i].ClassName == "ObjectValue" then
cancel = true
end
if not cancel then
local UniqueValNames = {}
local uniqueVals = {}
datastore:SetAsync(statstorage[i].Name.. player.UserId.. i, statstorage[i].Value)
local attributes = statstorage[i]:GetAttributes()
for name, value in pairs(attributes) do
table.insert(UniqueValNames,name)
table.insert(uniqueVals,value)
end
datastore:SetAsync(statstorage[i].Name.. player.UserId.. i.. "UN", UniqueValNames)
datastore:SetAsync(statstorage[i].Name.. player.UserId.. i.. "UV", uniqueVals)
table.insert(itemKeys,statstorage[i].Name.. player.UserId.. i)
end
end
end
datastore:SetAsync("TableItems",itemKeys)
print("Inventory Saved for ".. player.Name)
end
end
game.Players.PlayerRemoving:connect(function(player)
Save(player)
end)
game.Players.PlayerAdded:connect(function(player)
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Tools12")
repeat task.wait() until player:FindFirstChild("Inventory")
if datastore:GetAsync("TableItems") then
local items = datastore:GetAsync("TableItems")
for i=1, #items do
local NameOfKnife = datastore:GetAsync(items[i])
local UN = datastore:GetAsync(items[i].. "UN")
local UV = datastore:GetAsync(items[i].. "UV")
local StringVal = Instance.new("StringValue", player:WaitForChild("Inventory"))
StringVal.Name = NameOfKnife
StringVal.Value = NameOfKnife
if UN ~= nil then
for z=1, #UN do
local Name = UN[z]
local Value = UV[z]
if Name ~= nil then
StringVal:SetAttribute(Name, Value)
end
end
end
print("LoadedItem")
end
player:WaitForChild("Inventory"):SetAttribute("DoneLoadingInv", true)
print("Success on Giving Items")
else
player:WaitForChild("Inventory"):SetAttribute("DoneLoadingInv", true)
print("Started Game First Time Or Failed To Load Items")
end
end)
it usually takes 0.5 to 1 second to load one item, a string value is displayed inside a folder where the player is located at and also has attributes in the string value in the inventory folder. Also the string values have 5-10 attributes but are also saved but in a “Datastored” array {} to make it faster but it is still slow.