Hello yall!
I’ve been reading more about datastores and I would love to improve on mine:
Current DataStore for main Data:
local DSS = game:GetService("DataStoreService")
local PS = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local RNS = game:GetService("RunService")
local DataStore = DSS:GetDataStore("DimensionsDataDSS")
local PlayersDataFolder = SS:WaitForChild("PlayersData")
local Dimensions = require(game:GetService("ServerScriptService"):WaitForChild("Server").RollingSystem.RollingModuleServer.DimensionsModule)
--//Functions
if success then
print("Removed Nickname:", nickname)
end
function SetupPlayerData(Player)
task.wait(1)
local UserId = Player.UserId
local key = "Player_"..UserId
local PlayerDataFolder = PlayersDataFolder[Player.Name]
local DimensionsData = PlayerDataFolder.DimensionsData
local rolls = Instance.new("NumberValue")
rolls.Name = "Rolls"
rolls.Parent = DimensionsData
rolls.Value = 0
local dimensionequiped = Instance.new("StringValue")
dimensionequiped.Name = "DimensionEquiped"
dimensionequiped.Parent = DimensionsData
local titleequiped = Instance.new("StringValue")
titleequiped.Name = "TitleEquiped"
titleequiped.Parent = DimensionsData
local luckfactor = Instance.new("NumberValue")
luckfactor.Name = "LuckFactor"
luckfactor.Parent = DimensionsData
luckfactor.Value = 1
local DimensionsFolder = Instance.new("Folder")
DimensionsFolder.Name = "DimensionsFolder"
DimensionsFolder.Parent = DimensionsData
for Dimension, Value in pairs(Dimensions) do
local DimensionTypeValue = Instance.new("NumberValue")
DimensionTypeValue.Name = Value[1]
DimensionTypeValue.Parent = DimensionsFolder
DimensionTypeValue = 0
end
local Success, ReturnValue
Success, ReturnValue = pcall(DataStore.GetAsync, DataStore, key)
if Success then
if ReturnValue == nil then
ReturnValue = {}
end
for Index, Value in pairs(ReturnValue) do
if Index ~= "Rolls" and Index ~= "DimensionEquiped" and Index ~= "TitleEquiped" and Index ~= "LuckFactor" then
DimensionsFolder[Index].Value = if ReturnValue[Index] ~= nil then ReturnValue[Index] else 0
elseif Index == "Rolls" then
rolls.Value = ReturnValue[Index]
elseif Index == "DimensionEquiped" then
dimensionequiped.Value = ReturnValue[Index]
elseif Index == "TitleEquiped" then
titleequiped.Value = ReturnValue[Index]
elseif Index == "LuckFactor" then
luckfactor.Value = ReturnValue[Index]
end
end
else
Player:Kick("Your data might have been lost during the retrival process. If you have vaulable assets stored, please contact Support Service! [PlayerDimensionsDataDSS]")
end
end
function SaveData(Player)
local UserId = Player.UserId
local key = "Player_"..UserId
local PlayerDataFolder = PlayersDataFolder[Player.Name]
local DimensionsData = PlayerDataFolder.DimensionsData
local Data = {
Rolls = DimensionsData.Rolls.Value,
DimensionEquiped = DimensionsData.DimensionEquiped.Value,
TitleEquiped = DimensionsData.TitleEquiped.Value,
LuckFactor = DimensionsData.LuckFactor.Value
}
for i, Dimension in pairs(DimensionsData.DimensionsFolder:GetChildren()) do
Data[Dimension.Name] = Dimension.Value
end
local Success, ReturnValue
Success, ReturnValue = pcall(DataStore.UpdateAsync, DataStore, key, function()
return Data
end)
if Success then
print("Data successfully saved for "..Player.Name..". [DimensionsDataDSS]")
else
warn("Error found with DimensionsDataDSS. Reported User is "..Player.Name..". Please clip and report if seen this! [DimensionsDataDSS]")
end
end
function OnShutdown()
if RNS:IsStudio() then
task.wait(2)
else
local finished = Instance.new("BindableEvent")
local AllPlayers = PS:GetPlayers()
local LeftPlayers = #AllPlayers
for _, Player in ipairs(AllPlayers) do
coroutine.wrap(function()
SaveData(Player)
LeftPlayers -= 1
if LeftPlayers == 0 then
finished:Fire()
end
end)
end
finished.Event:Wait()
end
end
--//Connections
PS.PlayerAdded:Connect(function(Player)
SetupPlayerData(Player)
while Player ~= nil do
SaveData(Player)
wait(20)
end
end)
PS.PlayerRemoving:Connect(SaveData)
game:BindToClose(OnShutdown)
Since this datastore really doesn’t support data-loss or server locking for multi-server editing, I really need to add parts to achieve this. How can I add these features to my current datastore?
+I did look at ProfileService and its very reliable, but I want to learn how to create something along the lines of that. I’m not really asking for entire script, but just maybe some idea and parts I can add and understand so I can improve how I implement saving and retrieving data better!
I do want to mainly implement server-locking and make sure that the latest of data with verification is edited. I also want to make sure that players can revert to their last version of data if corrupted or lost.