How do you make a script that saves the players leaderstats data everyone couple seconds or minutes?

I was trying to make a script where the leaderstats data saves every couple minutes or seconds, but I can’t quite seem to figure out how this is done. If someone could tell me or show me how it’s done I would really appreciate it.

This is the script I have make so far. Only saves when player leaves:

local DataStoreService = game:GetService("DataStoreService")
local LeaderstatsData1 = DataStoreService:GetDataStore("LeaderstatsData1")

game.Players.PlayerAdded:Connect(function(player) -- Gets the player and connects it with the game so that you can add data into it.
	local leaderstats ="Folder", player) -- Makes a folder called leaderstats inside the player.
	leaderstats.Name = "leaderstats" -- Names the folder "leaderstats" (required name otherwise won't work.)
	local points ="IntValue", leaderstats) -- Creates an int value inside the leaderstats folder.
	points.Name = "Points" -- Names the int value it will show up on the 'leaderboard' (will be visible).
	points.Value = 0 -- This sets the value of the 'int value' to whatever you want.
	local xp ="IntValue", leaderstats) -- Creates an int value inside the leaderstats folder.
	xp.Name = "XP" -- Names the int value it will show up on the 'leaderboard' (will be visible).
	local PlayerUserId = "Player_"..player.UserId
	-- Load Data when player joins.
	local Data1
	local success, errormessage = pcall(function()
		Data1 = LeaderstatsData1:GetAsync(PlayerUserId)
	-- Load the players data is success
	if success then
		if Data1 then
			points.Value = Data1.Points
			xp.Value = Data1.XP

-- Saving data when player is leaving.
	local PlayerUserId = "Player_"..player.UserId
	local Data1 = {
		Points = player.leaderstats.Points.Value;
		XP = player.leaderstats.XP.Value
	local success, errormessage = pcall(function()
		LeaderstatsData1:SetAsync(PlayerUserId, Data1)
	if success then
		print("Player data saved!")
		print("There was an error while saving data!")

I don’t know why you’d want to do this, because it comes with performance issues. But to achieve this you can simply loop through all players every set amount of seconds and save their data.

local Players = game:GetService("Players")

local function SaveData(player)

while task.wait(10) do
	for _, player in ipairs(Players:GetPlayers()) do

Okay thank you, I did think that it would affect performance I wasn’t actually going to use this but I just wanted to know how to do this because maybe if I wanted to use it for something else. Thanks anyway.