This is the server side of my saving method for my character customization menu and eventually inventory. I am not too experienced with writing datastore code but is this code here clean and efficient enough? I am not looking for perfection, but a good enough structure that other devs who look at it do not react with utter disbelief.
DataStore2.Combine("Data", "Eyes", "Nose", "Mouth", "Brows")
DataStore2.Combine("Data", "SkinColor")
--Services
local Remotes = game:GetService("ReplicatedStorage").Remotes
local Facial = Remotes.PlayerData.FaceFeatures
local Body = Remotes.PlayerData.Body
local Server2Client = Remotes.Technic.Server2Client
--cam vars
local setcam = Server2Client.SetCustomizationCam
--datavars
--defaults
local Defaultval = 1
--remotes
local skinColorREM = Body.SkinColor
--facialfeatures
local BrowsREM = Facial.Brows
local EyesREM = Facial.Eyes
local NoseREM = Facial.Nose
local MouthREM = Facial.Mouth
game.Players.PlayerAdded:Connect(function(player)
setcam:FireClient(player)
------datastores------
local SkinColorDataStore = DataStore2("SkinColor", player)
--facialdatastores
local BrowsDataStore = DataStore2("Brows", player)
local EyesDataStore = DataStore2("Eyes", player)
local NoseDataStore = DataStore2("Nose", player)
local MouthDataStore = DataStore2("Mouth", player)
--Values
local Folder = Instance.new("Folder", player)
Folder.Name = "Stats"
local CharacterPreferences = Instance.new("Folder", Folder)
CharacterPreferences.Name = "CharacterPreferences"
local FacialFeatures = Instance.new("Folder", Folder)
FacialFeatures.Name = "FacialFeatures"
---Skincolor
local SkinColor = Instance.new("IntValue", CharacterPreferences)
SkinColor.Name = "SkinColor"
--Brows
local Brows = Instance.new("IntValue", FacialFeatures)
Brows.Name = "Brows"
--Eyes
local Eyes = Instance.new("IntValue", FacialFeatures)
Eyes.Name = "Eyes"
---Nose
local Nose = Instance.new("IntValue", FacialFeatures)
Nose.Name = "Nose"
---Mouth
local Mouth = Instance.new("IntValue", FacialFeatures)
Mouth.Name = "Mouth"
------updates------
---SkinColor
local function updateSkinColor(updatedValue)
SkinColor.Value = SkinColorDataStore:Get(updatedValue)
end
updateSkinColor(Defaultval)
SkinColorDataStore:OnUpdate(updateSkinColor)
---Brows
local function updateBrows(updatedValue)
Brows.Value = BrowsDataStore:Get(updatedValue)
end
updateBrows(Defaultval)
BrowsDataStore:OnUpdate(updateBrows)
---Eyes
local function updateEyes(updatedValue)
Eyes.Value = EyesDataStore:Get(updatedValue)
end
updateEyes(Defaultval)
BrowsDataStore:OnUpdate(updateEyes)
--Nose
local function updateNose(updatedValue)
Nose.Value = NoseDataStore:Get(updatedValue)
end
updateNose(Defaultval)
BrowsDataStore:OnUpdate(updateNose)
---Mouth
local function updateMouth(updatedValue)
Mouth.Value = MouthDataStore:Get(updatedValue)
end
updateMouth(Defaultval)
BrowsDataStore:OnUpdate(updateMouth)
end)
------Listeners------
---SkinColor
skinColorREM.OnServerEvent:Connect(function(player, value)
local SkinStore = DataStore2("SkinColor", player)
SkinStore:Set(value, Defaultval)
end)
---Brows
BrowsREM.OnServerEvent:Connect(function(player, value)
local BrowsStore = DataStore2("Brows", player)
BrowsStore:Set(value, Defaultval)
end)
---Eyes
EyesREM.OnServerEvent:Connect(function(player, value)
local EyesStore = DataStore2("Eyes", player)
EyesStore:Set(value, Defaultval)
end)
---Nose
NoseREM.OnServerEvent:Connect(function(player, value)
local NoseStore = DataStore2("Nose", player)
NoseStore:Set(value, Defaultval)
end)
---Mouth
MouthREM.OnServerEvent:Connect(function(player, value)
local MouthStore = DataStore2("Mouth", player)
MouthStore:Set(value, Defaultval)
end)