I am currently working on a Datastore module. I have created most of the code, but I simply need a script that can repeatedly save, like an Autosave.
I want the script to be a child of the main Module (so a ModuleScript, if that’s possible). I would really love it if I could incorporate it into another script using coroutines or something like that. I honestly just don’t know what to do in this situation, so I’d love it if some people could help give me ideas, or explain ways that it could be done.
This is the script at the moment:
local DS = require(script.Parent)
local interval = 300 --The amount of time (in seconds) between each autosave
local Manager = {}
Manager.__index = Manager
local function Autosave()
local d, t, s = DS.BulkSave()
if d == true then
print("Autosave Successful! "..tostring(s).."/"..tostring(t).." data pieces successfully saved!")
end
end
while true do
wait(interval)
Autosave()
end
return Manager
As you can see the module is called Manager, because I want it to be more than just an Autosaver, but at the moment that is all that it can do. As well, the only way to get it started (that I know of) is with require(). The script which ends up indefinitely yielding the script that requires it, which is a problem.
well its hard to understand what you want, but the main problem i see here is
while true do
wait(interval)
Autosave()
end
it will if not put in something not allow manager to ever be returned.
im not really sure how your saving works, but using the current script i guess
local DS = require(script.Parent)
local interval = 300 --The amount of time (in seconds) between each autosave
local Manager = {}
Manager.__index = Manager
local function Autosave()
local d, t, s = DS.BulkSave()
if d == true then
print("Autosave Successful! "..tostring(s).."/"..tostring(t).." data pieces successfully saved!")
end
end
function Manager.AutoSave()
while true do
wait(interval)
Autosave()
end
end
return Manager
then you would call autosave to i guess call autosave from some other script like the main module, also just a random question, how would you even know the player to autosave for in this situation?
edit: this is another slightly neater way using my thingy, at least to me, to do it, it doesnt change much but i think it would be ok
new script thingy
local DS = require(script.Parent)
local interval = 300 --The amount of time (in seconds) between each autosave
local Manager = {}
Manager.__index = Manager
function Manager.Autosave()
while wait(interval) do
local d, t, s = DS.BulkSave()
if d == true then
print("Autosave Successful! "..tostring(s).."/"..tostring(t).." data pieces successfully saved!")
end
end
end
return Manager
The main module collects and caches data when it is “Saved” by the client or a server script. The autosave function connects to a function in the main module called “BulkSave” which iterates through the cache and saves everything within it. In the future I will probably change it so that it only saves things that have been updated, but currently thats how it works. It doesn’t need to know who to autosave for, because that is all handled by the “BulkSave” function which already has the keys from the cache.