How to make autosave an option?

Hi, so I recently have made an autosave script, but my game also has a save button. Is there any way I can make autosaving an option? Thanks.

Autosave script:

local DataStoreService = game:GetService("DataStoreService")
local CashStore = DataStoreService:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = leaderstats

	local data
	local Success, Error = pcall(function()
		data = CashStore:GetAsync(Player.UserId.."-cash")
	end)

	if Success then
		Cash.Value = data
		print("Data Found!")
	else
		warn(Error)
	end
end)
local queue = 0
game.Players.PlayerRemoving:Connect(function(Player)
	queue += 1
	local Sucess, Error = pcall(function()
		CashStore:SetAsync(Player.UserId.."-cash", Player.leaderstats.Cash.Value)
	end)

	if Sucess then
		print("Data Saved!")
		queue -= 1
	else
		warn(Error)
	end
end)

game:BindToClose(function()
	repeat wait() until queue == 0
end)

Save button script:

local ds = game:GetService("DataStoreService")
local rc = ds:GetDataStore("RebirthsSave")
local lc = ds:GetDataStore("Levels")
local ss = ds:GetDataStore("SpeedValues")
local qc = ds:GetDataStore("PlayerQuests")
local gs = ds:GetDataStore("GemsSave")

local ReplicatedStorage = game.ReplicatedStorage

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.Quests:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end


ReplicatedStorage.Save.OnServerEvent:Connect(function(player)
	local success, errormessage = pcall(function()
	rc:SetAsync(player.UserId, player.leaderstats.Rebirths.Value)
	lc:SetAsync(player.UserId, player.leaderstats.Levels.Value)
	ss:SetAsync(player.UserId, player.Data.attackspeed.Value)
	gs:SetAsync(player.UserId, player.leaderstats.Gems.Value)
		
		local plrstats = create_table(player)
	qc:SetAsync(player.UserId, plrstats)
	end)
	
	if success then
		print("Success For Saving")
	else
		warn(errormessage)
	end
end)




2 Likes

EDIT:
I really am sorry. There used to be an article that not only covered the basics of how most people structure datastore systems but also covered cacheing and autosaving. I can no longer find it however or it was removed. Either way it’s a bummer.

2 Likes

If you’d want to establish an autosaving scheme, you can write a function() for updating/saving data, and set it in a repeat wait() updateData() until false, alongside others. Here’s a sample:

local function updateData(plr)
    --your code here
end
coroutine.wrap(function() --coroutine so it doesn't interrupt other code by yielding
    repeat wait(10) --(amount of seconds until the next autosave)
    for _, i in pairs(game.Players:GetPlayers()) do --gets all players
        updateData(i) --updates data
    end
    until false
end)()

To make it an option, you can create a BoolValue, parent it to the player, then check the value of it.

--in the function
if not plr.BoolValueName.Value then return end
3 Likes

Any PlayerData should always be automatically saved, this includes any kind of leaderstat. Obviously it’s up to you, but the standard is now that online game data autosaves (plus it’s so easy to implement, manual saving doesn’t even make sense) and players will be very confused and upset if they lose data because of it.

The only time when manual saving can occur is with a plot system where the player can potentially save multiple plots or builds. Or a similar system. (like in build a boat for treasure, but notice how in build a boat it’s only the boat that manually saves, all the block data auto saves like it should)

2 Likes

Oh yeah this could probably work, thanks.

Yeah that does make alot of sense