Hey, i wanted to ask what can i improve in this simple module, i made it to make data store more sorted by sections like tools, inventory ect.
if there is anything to improve, please tell me that, thx for help
and here is a code, there is main script that calls
local Functions = {}
local DataStoreService = game:GetService("DataStoreService")
local DS1 = DataStoreService:GetDataStore("DS1")
local Inventory = game:GetService("ReplicatedStorage").Inventories
local ValueTypes = {
["boolean"] = "BoolValue",
["string"] = "StringValue",
["number"] = "NumberValue",
["integer"] = "IntValue"
}
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
--[[Creates new value under given folder]]
local function CreateNewValue(name: string, value: any, parent: Instance)
local newObject = Instance.new(ValueTypes[typeof(value)])
newObject.Name = name
newObject.Value = value
newObject.Parent = parent
end
--[[Creates folder with player data in Replicated Storage]]
local function LoadData(player: Player, data: {})
local PlayerInventory = Inventory:FindFirstChild(player.UserId)
for name, value in data do
if typeof(value) == "table" then
local newFolder = Instance.new("Folder")
newFolder.Name = name
newFolder.Parent = PlayerInventory
for objectName, newValue in value do
CreateNewValue(objectName,newValue,newFolder)
end
else
CreateNewValue(name,value,PlayerInventory)
end
end
end
--[[Serializes inventory into table]]
local function SerializeData(PlayerInventory: Folder) : {}
local data = {}
for _, object in PlayerInventory:GetChildren() do
if object:IsA("Folder") then
data[object.Name] = {}
for _, value in object:GetChildren() do
data[object.Name][value.Name] = value.Value
end
else
data[object.Name] = object.Value
end
end
return data
end
--[[Creates new inventory when player joins for the first time]]
function Functions.newInventory(player: Player)
local newInventory = Instance.new("Folder")
newInventory.Name = player.UserId
newInventory.Parent = Inventory
local PlayerInventory = Instance.new("Folder")
PlayerInventory.Name = "Inventory"
PlayerInventory.Parent = newInventory
local Id = Instance.new("IntValue")
Id.Name = "Id"
Id.Value = 0
Id.Parent = newInventory
end
--[[loads player's data]]
function Functions:Load(player: Player)
local state = true
local data
local key = player.UserId
local succes,err = pcall(function()
data = DS1:GetAsync(key)
end)
if succes and data then
local PlayerInventory = Instance.new("Folder")
PlayerInventory.Name = tostring(player.UserId)
PlayerInventory.Parent = Inventory
LoadData(player,data)
elseif data == nil then
state = false
else
warn("Error while loading data... ", err)
end
return state
end
function Functions:Save(player: Player, autoSave: boolean)
local PlayerInventory = Inventory:FindFirstChild(player.UserId)
local DataToSave = SerializeData(PlayerInventory)
local key = player.UserId
if not autoSave then PlayerInventory:Destroy() end
local succes, err = pcall(function()
DS1:UpdateAsync(key, function(oldData)
if not oldData then
DataToSave.Id = 0
return DataToSave
elseif oldData.Id == DataToSave.Id then
DataToSave.Id += 1
return DataToSave
else
return nil
end
end)
end)
if succes then
return true
else
warn(err)
return false
end
end
return Functions