local DataStoreModule = require(11671168253)
local template = {
Level = 0,
Coins = 0,
Inventory = {},
DeveloperProducts = {},
}
-- This function is currently not used
local function StateChanged(state, dataStore)
while dataStore.State == false do
if dataStore:Open(template) ~= "Success" then task.wait(6) end
end
end
game.Players.PlayerAdded:Connect(function(player)
local dataStore = DataStoreModule.new("Player", player.UserId)
-- dataStore.StateChanged:Connect(StateChanged) -- dont connect to statechanged here
--StateChanged(dataStore.State, dataStore) -- dont try to open the datastore object
-- Temporary
datastore:Reconcile(template) -- reconcile the template onto datastore.Value
dataStore.StateChanged:Fire(true, datastore) -- fire statechanged to true to trick other scripts
-- End Temporary
end)
game.Players.PlayerRemoving:Connect(function(player)
local dataStore = DataStoreModule.find("Player", player.UserId)
if dataStore ~= nil then dataStore:Destroy() end
end)
now to use the datastore you do
local dataStore = DataStoreModule.find("Player", player.UserId)
if dataStore == nil then return end
-- if dataStore.State ~= true then return end -- temporarily remove state checks
dataStore.Value.Level += 1
or another option is to just make a make module that acts like the datastore module
local dataStores = {}
local module = {}
module.__index = module
function module.new(name, scope, key)
if key == nil then key, scope = scope, "global" end
local id = name .. "/" .. scope .. "/" .. key
if dataStores[id] ~= nil then return dataStores[id] end
local self = setmetatable({}, module)
self.Id = id
self.State = false
dataStores[id] = self
return self
end
function module.find(name, scope, key)
if key == nil then key, scope = scope, "global" end
local id = name .. "/" .. scope .. "/" .. key
return dataStores[id]
end
function module:Open(template)
if self..Value == nil then
self.Value = Clone(template)
elseif type(self.Value) == "table" and type(template) == "table" then
Reconcile(self.Value, template)
end
self.State = true
end
function module:Destroy()
dataStores[self.Id] = nil
self.State = nil
end
function Clone(original)
if type(original) ~= "table" then return original end
local clone = {}
for index, value in original do clone[index] = Clone(value) end
return clone
end
function Reconcile(target, template)
for index, value in template do
if type(index) == "number" then continue end
if target[index] == nil then
target[index] = Clone(value)
elseif type(target[index]) == "table" and type(value) == "table" then
Reconcile(target[index], value)
end
end
end
return module
or another option is to modify the original module
Lock = function(dataStore, attempts)
local success, value, id, lockTime, lockInterval, lockAttempts = nil, nil, nil, nil, dataStore.__public.LockInterval, dataStore.__public.LockAttempts
for i = 1, attempts do
if i > 1 then task.wait(1) end
lockTime = os.clock()
--success, value = pcall(dataStore.MemoryStore.UpdateAsync, dataStore.MemoryStore, "Id", function(value) id = value return if id == nil or id == dataStore.__public.UniqueId then dataStore.__public.UniqueId else nil end, lockInterval * lockAttempts + 30)
success, value = true, "LockId" -- ADD this
if success == true then break end
end
if success == false then return "Error", value end
if value == nil then return "Locked", id end
dataStore.LockTime = lockTime + lockInterval * lockAttempts
dataStore.ActiveLockInterval = lockInterval
dataStore.__public.AttemptsRemaining = lockAttempts
return "Success"
end
Unlock = function(dataStore, attempts)
local success, value, id = nil, nil, nil
for i = 1, attempts do
if i > 1 then task.wait(1) end
--success, value = pcall(dataStore.MemoryStore.UpdateAsync, dataStore.MemoryStore, "Id", function(value) id = value return if id == dataStore.__public.UniqueId then dataStore.__public.UniqueId else nil end, 0)
success, value = true, "LockId" -- ADD this
if success == true then break end
end
if success == false then return "Error", value end
if value == nil and id ~= nil then return "Locked", id end
return "Success"
end
Load = function(dataStore, attempts)
local success, value, info = nil, nil, nil
for i = 1, attempts do
if i > 1 then task.wait(1) end
--success, value, info = pcall(dataStore.DataStore.GetAsync, dataStore.DataStore, dataStore.__public.Key)
success, value = true, nil -- ADD this
if success == true then break end
end
if success == false then return "Error", value end
if info == nil then
dataStore.__public.Metadata, dataStore.__public.UserIds, dataStore.__public.CreatedTime, dataStore.__public.UpdatedTime, dataStore.__public.Version = {}, {}, 0, 0, ""
else
dataStore.__public.Metadata, dataStore.__public.UserIds, dataStore.__public.CreatedTime, dataStore.__public.UpdatedTime, dataStore.__public.Version = info:GetMetadata(), info:GetUserIds(), info.CreatedTime, info.UpdatedTime, info.Version
end
if type(dataStore.__public.Metadata.Compress) ~= "table" then
dataStore.__public.Value = value
else
dataStore.__public.CompressedValue = value
local decimals = 10 ^ (dataStore.__public.Metadata.Compress.Decimals or 3)
dataStore.__public.Value = Decompress(dataStore.__public.CompressedValue, decimals)
end
return "Success"
end
Save = function(proxy, attempts)
local dataStore = getmetatable(proxy)
local deltaTime = os.clock() - dataStore.SaveTime
if deltaTime < dataStore.__public.SaveDelay then task.wait(dataStore.__public.SaveDelay - deltaTime) end
dataStore.__public.Saving:Fire(dataStore.__public.Value, proxy)
local success, value, info = nil, nil, nil
if dataStore.__public.Value == nil then
for i = 1, attempts do
if i > 1 then task.wait(1) end
--success, value, info = pcall(dataStore.DataStore.RemoveAsync, dataStore.DataStore, dataStore.__public.Key)
success = true -- ADD this
if success == true then break end
end
if success == false then dataStore.__public.Saved:Fire("Error", value, proxy) return "Error", value end
dataStore.__public.Metadata, dataStore.__public.UserIds, dataStore.__public.CreatedTime, dataStore.__public.UpdatedTime, dataStore.__public.Version = {}, {}, 0, 0, ""
elseif type(dataStore.__public.Metadata.Compress) ~= "table" then
dataStore.Options:SetMetadata(dataStore.__public.Metadata)
for i = 1, attempts do
if i > 1 then task.wait(1) end
--success, value = pcall(dataStore.DataStore.SetAsync, dataStore.DataStore, dataStore.__public.Key, dataStore.__public.Value, dataStore.__public.UserIds, dataStore.Options)
success = true -- ADD this
if success == true then break end
end
if success == false then dataStore.__public.Saved:Fire("Error", value, proxy) return "Error", value end
dataStore.__public.Version = value
else
local level = dataStore.__public.Metadata.Compress.Level or 2
local decimals = 10 ^ (dataStore.__public.Metadata.Compress.Decimals or 3)
local safety = if dataStore.__public.Metadata.Compress.Safety == nil then true else dataStore.__public.Metadata.Compress.Safety
dataStore.__public.CompressedValue = Compress(dataStore.__public.Value, level, decimals, safety)
dataStore.Options:SetMetadata(dataStore.__public.Metadata)
for i = 1, attempts do
if i > 1 then task.wait(1) end
--success, value = pcall(dataStore.DataStore.SetAsync, dataStore.DataStore, dataStore.__public.Key, dataStore.__public.CompressedValue, dataStore.__public.UserIds, dataStore.Options)
success = true -- ADD this
if success == true then break end
end
if success == false then dataStore.__public.Saved:Fire("Error", value, proxy) return "Error", value end
dataStore.Version = value
end
dataStore.SaveTime = os.clock()
dataStore.__public.Saved:Fire("Saved", dataStore.__public.Value, proxy)
return "Saved", dataStore.__public.Value
end