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”)

game.Players.PlayerAdded:Connect(function(player)
	
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	
	local cash = Instance.new ("IntValue",leaderstats)
	cash.Name = "Cash"
	cash.Value = 0
	
	
	local kills = Instance.new("IntValue", leaderstats)
	kills.Name = "Kills"
	
	
	
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function(killed)
			
			
			
			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
			end
		end)
	end)
end)

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

GetData()
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)
	end)
	
	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
	end
end

Then a function for Updating it to place within a loop

UpdateData()
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
		end)
	end)
end

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
		UpdateData(Player)
	end

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.

Thanks!

Is that a DataStore2 script?

I’m only familiar with SetAsync and GetAsync.