How do I make an "Autosave" for this leaderstats script?

Hello! So, I’m currently working on a game and I’m trying to make a “Cash” and “Kills” leaderstat, here’s the script:

(It’s put in game > ServerScriptService > folder named “leaderstats” > and then the script named “leaderstats”)

	local leaderstats ="Folder",player)
	leaderstats.Name = "leaderstats"
	local cash = ("IntValue",leaderstats)
	cash.Name = "Cash"
	cash.Value = 0
	local kills ="IntValue", leaderstats)
	kills.Name = "Kills"
			local CreatorTag = character.Humanoid:FindFirstChild("creator")
			if CreatorTag and CreatorTag.Value then
				local stats = CreatorTag.Value:WaitForChild("leaderstats")
				stats["Cash"].Value = stats["Cash"].Value + 20
				stats["Kills"].Value = stats["Kills"].Value + 1

So, I want to make an autosave for this, but I’m pretty bad at scripting. I need help, please tell me how to make one!

Thanks for reading! Have a great day.

(Edit: the two leaderstats are "Cash, and Kills. Each time you get a kill, you get +20 cash. And yes, I didn’t make this script. It came from a free model.)

1 Like

You want to save at intervals of no less than 30 seconds. I also see that you haven’t written any code about Datastores no variables defined that are needed to use them as well

First, you need to call DataStoreService | Roblox Creator Documentation to use Datastores then you need to get your datastore using DataStoreService | Roblox Creator Documentation

For my example the two variables that are needed are

local DatastoreService = game:GetService("DataStoreService")
local PlayerDataStore = DatastoreService:GetDataStore("PlayerData")

Second, What u will need
you need to retrieve data

function GetData(Player)
	if not Player return end -- if player is not in game end function
	local Data
	local Success, Response = pcall(function()
		Data = PlayerDataStore:GetAsync(Player.UserId)
	if Success and not Data then --// If Data Retrival Sucessful and Data == nil
		warn(Player.Name .. " has no Data ... Setting Up Datastore")
		local DataTable = PlayerDataTable
		return DataTable
	else --// If Data Retrival Sucessful and Data ~= nil
			warn(Player.Name .. " hasData ... Data Retrieval Successful")
		return Data

Then a function for Updating it to place within a loop

function UpdateData(Player)
	if not Player return end -- if player is not in game end function

	local LeaderStatsFolder = Player.leaderstats 

	local Cash = LeaderStatsFolder.Cash
	local Kills = LeaderStatsFolder.Kills

	local DataTable = PlayerDataTable

	local Success, Response = pcall(function()
		PlayerDataStore:UpdateAsync(Player.UserId, function(OldTable)
			local NewTable = {
			Cash = Cash.Value,
			Kills = Kills.Value,
			return NewTable

The loop for your AutoSave

Data Update Loop

Within PlayerAdded you would place this loop at very bottom

	while true do
		wait(30) -- // If wait is anyless than 30 seconds it will be overkill

Thanks! By the way, as you may have read I am a beginner scripter.
So yes, thanks for the help. But, where do I exactly put this?

If you could make this into one script for me that would be awesome!
It may be too much, so you don’t have to.


Is that a DataStore2 script?

I’m only familiar with SetAsync and GetAsync.