I have an obby and I want to save the stage that they’re on. However, I get this warning sometimes.
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)
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.
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
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).
Don’t use the parent parameter with Instance.new if you are assigning its properties first.
Proper way: Create, assign, parent.
You can create a temporary datastore cache which you can check if their data has actually been changed before setting it.
Save the data on an interval, ex every 2 minutes.
Get the data when they are joining (.PlayerAdded).
Save the data when the player is leaving (.PlayerRemoving).
You should also include DataModel:BindToClose.
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.
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.