hi, I’m creating a core script server that manages a module that I created just for saving data. is a module that temporarily saves data within a table until the player exits or the server closes. This is the form
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MASTERDATA")
local RunService = game:GetService("RunService")
local serverData = {}
------define globals -------------------------
--TODO output notificator and debugger
--------------------------------------------
------ SAVE DATA ---------------------------
serverData.SaveData= {
prototype = {
number = 0
},
metatable = {} --TODO da modificare la table
}
serverData.SaveData.metatable.__index = serverData.SaveData.prototype
setmetatable(serverData.SaveData , {
__call = function(plr)
local newData = {}
setmetatable(newData,serverData.SaveData.metatable)
return newData
end
})
function serverData.SaveData:Update(plr,data)
local oldData = {}
setmetatable(oldData,serverData.SaveData.metatable)
return oldData
end
---------------------------------------------
------ GAME DATA ---------------------------
serverData.GameData= {
prototype = {},
metatable = {} --TODO da modificare la table
}
serverData.GameData.metatable.__index = serverData.GameData.prototype
setmetatable(serverData.GameData , {
__call = function(plr)
local newData = {}
setmetatable(newData,serverData.GameData.metatable)
return newData
end
})
--------------------------------------------------------------------------------
serverData.sData = {}
serverData.gData = {}
function serverData:assemble(parent,serverRef)
for i,v in pairs(parent:GetDescendants()) do
if v == serverRef and v.ClassName == "ModuleScript" then
serverData[v.Name] = require(v)
end
end
end
function serverData:newPlrData(plr)
local key
local success, err = pcall(function()
key = DataStore:GetAsync(plr.UserId)
end)
if success then
if key then
self.sData[plr.Name] = self.SaveData:Update(plr,key)
else
self.sData[plr.Name] = self.SaveData(plr)
end
self.gData[plr.Name] = self.GameData(plr)
else
if RunService:IsStudio() then
self.sData[plr.Name] = self.SaveData(plr)
self.gData[plr.Name] = self.GameData(plr)
else
plr:Kick("Errore nel caricamente del DataStore dal server / Error loading Datastore from server")
end
end
if plr then
return self.sData[plr.Name] , self.gData[plr.Name]
end
end
function serverData:removePlrData(plr)
local sData , gData = self.sData[plr.Name],self.gData[plr.Name]
if sData and gData then
local success , error = pcall(function()
print(self.sData)
print(sData)
local success, err = pcall(function()
DataStore:SetAsync(plr.UserId,sData )
end)
if not success then
warn(error)
end
end)
end
self.sData[plr.Name] = nil
self.gData[plr.Name] = nil
end
return serverData
The require of this and its methods are managed entirely by the Core server script in this way:
function main()
Server:assemble(ServerScriptService,ServerInstance)
game:BindToClose(function()
for i_ , player in pairs(Players:GetPlayers())do
local CloseBindThread = coroutine.wrap(function()
Server:removePlrData(player)
end)()
end
end)
end
Players.PlayerRemoving:Connect(function(player)
Server:removePlrData(player)
--some code ...
end)
----------------------------------------------
Players.PlayerAdded:Connect(function(player)
--some code ...
local sData , gData = Server:newPlrData(player)
Server.sData[player.Name].number += 5
print(sData.number)
end)
------------------------------------------------
main()
On roblox studio don’t go datastores so I did publish to try it but the data is not saved. thank you in advance