How often should I save data?

I have an obby and I want to save the stage that they’re on. However, I get this warning sometimes.
image
I am aware that this is because I’m saving too often, so when should I save? Currently, it saves whenever the player beats a stage.

local dataStore = game:GetService("DataStoreService"):GetDataStore("AdventureObbyStage")

game.Players.PlayerAdded:Connect(function(p)
	
	local leaderstats = Instance.new("Folder", p)
	leaderstats.Name = "leaderstats"
	
	local stage = Instance.new("NumberValue", leaderstats)
	stage.Name = "Stage"
	
	local SaveKey = "Player_"..p.UserId
	local data
	
	local s, e = pcall(function()
		data = dataStore:GetAsync(SaveKey)
	end)
	
	if s then
		stage.Value = data
	else
		print(e)
	end
	if stage.Value == 0 then
		stage.Value = 1 -- basically setting the initial value if the player hadn't ever joined the game
	end
	
	stage.Changed:Connect(function()
		local success, errorM = pcall(function()
			dataStore:SetAsync(SaveKey, stage.Value)
		end)
		if success then
			print("yes")
		else
			print(errorM)
		end
	end)
end)
2 Likes

I should say you should technically leave the datastore alone and just shorten its database a tad bit. Too much datastore requests will eventually break it.

Edit: You can always shorten each datastore table in which case would stop sending the requests.

2 Likes

And how would I do this at all? [30 char]

1 Like

First thing I would do is save every 30 seconds and not when the value is changed.
Times to Save:
- Every 30 seconds or 60 seconds
- When the Player leaves the game
- When the server shuts down

1 Like
  1. Never save data when a value is changed, as this can throttle if it’s changing too fast (less than the datastore limit per key, which is 6 seconds).

  2. Don’t use the parent parameter with Instance.new if you are assigning its properties first.
    Proper way: Create, assign, parent.

  3. You can create a temporary datastore cache which you can check if their data has actually been changed before setting it.

  4. Save the data on an interval, ex every 2 minutes.

  5. Get the data when they are joining (.PlayerAdded).
    Save the data when the player is leaving (.PlayerRemoving).
    You should also include DataModel:BindToClose.

More about BindToClose:

4 Likes

You can use Datastore2, you can send unlimited requests without a single cooldown. Pretty cool

3 Likes

Where did you get that information, because I’m interested in using data store 2 without worrying about sending to many requests that the regular datastore doesn’t save it.

How do I use datastore 2? and is there a limit or smn?

I use ProfileService now, it also allows you to send unlimited requests (aka caching) and only saves the data upon leaving. Also has custom session lock implemented.