How do I make make my DataStore save?

Firstly I have no idea how to make my DataStore save player data in timed intervals, and secondly I don’t know what the time limit is to saving data(I think its 60 seconds but I’m not sure.) This is my DataStore :


local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")

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

	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = player.leaderstats
	local data
	local success, errormessage = pcall(function()


		data = ClickDataStore:GetAsync(player.userId.."Clicks")
	end)

	if success then
		Clicks.Value = data
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end

end)```

Easy. Just use a while wait() do loop. add the following code to the bottom of your player added event.

while wait(60) do
	local success, err = pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)	
	end)
	
	if success then
		print("Data auto saved")
	else
		warn(err)
	end
end
2 Likes

Is there a way of making it save faster or is that the fastest?

Yep. Just change the wait interval.

Is there a limit to how fast you can save?

Read this article. It tells you all of the limitations of datastores, common errors, and other stuff. This should answer all of your questions. Data Stores

So is this what the final script should look like?


local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")

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


	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = player.leaderstats
	local data
	local success, errormessage = pcall(function()

		data = ClickDataStore:GetAsync(player.userId.."Clicks")
		while wait(60) do
			local success, err = pcall(function()
				ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)	
			end)

			if success then
				print("Data auto saved")
			else
				print("There was an error when saving data")
				warn(error)
			end
		end
	end)

	if success then
		Clicks.Value = data
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end

end)

No, put the while wait() do loop at the bottom, not in the pcall

No, put it at the bottom. Here, I fixed it for you.

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


	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = stats
	local data
	local success, errormessage = pcall(function()
		data = ClickDataStore:GetAsync(player.userId.."Clicks")
	end)

	if success then
		Clicks.Value = data
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
	
	while wait(60) do
		local success, err = pcall(function()
			ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)	
		end)

		if success then
			print("Data auto saved")
		else
			print("There was an error when saving data")
			warn(error)
		end
	end
end)
1 Like

There is a error: W000: (12,10) Unknown global ‘ClickDataStore’

That I believe wouldn’t work; try doing coroutine.wrap(function()

I’m not sure but I think it would be better to performance still;

Or just spawn() since it’s simpler;

1 Like

It’s because “ClickDataStore” doesn’t exist. All I sent was the PlayerAdded event. You replaced your entire script…

1 Like

So like this:


local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")

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


	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = stats
	local data
	local success, errormessage = pcall(function()
		data = ClickDataStore:GetAsync(player.userId.."Clicks")
	end)

	if success then
		Clicks.Value = data
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end

	while wait(60) do
		local success, err = pcall(function()
			ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)	
		end)

		if success then
			print("Data auto saved")
		else
			print("There was an error when saving data")
			warn(error)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end

end)
1 Like

Yes, that’s correct. Finally lol. Although, you did make a small mistake. You’re trying to print “error”, but the variable name is “err”.

1 Like

Like this?


local ClickDataStore = DataStoreService:GetDataStore("ClickDataStore")

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


	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Parent = stats
	local data
	local success, errormessage = pcall(function()
		data = ClickDataStore:GetAsync(player.userId.."Clicks")
	end)

	if success then
		Clicks.Value = data
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end

	while wait(60) do
		local success, error = pcall(function()
			ClickDataStore:SetAsync(player.UserId.."Clicks", Clicks.Value)	
		end)

		if success then
			print("Data auto saved")
		else
			print("There was an error when saving data")
			warn(error)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage =  pcall(function()
		ClickDataStore:SetAsync(player.UserId.."Clicks",player.leaderstats.Clicks.Value)
	end)

	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end

end)

yep. That should be everything. Let me know if there are any other errors, but there shouldn’t be.

2 Likes