Currently saving on leave but players sometimes lose their stats.
id save every death and on leave, for ranks and points etc. If you are saving weapon loadouts, save every spawn/deploy.
Hi there,
A general rule of thumb :
- When players leave
- Auto save like each 3 minutes
- After they did a transaction for ROBUX / Real life currency
Losing stats is something that you have to check out by posting your code for the datastore.
Also please try to use the search function next time, there are plenty of topics about this subject with exactly the same question.
- Velibor
As others have said you should save whenever a player leaves, after transactions, when the game closes using BindToClose, and every so often. Personally I save data every 5 minutes because that seems to be a reasonable amount of time since datastores take roughly a minute to update and there’s no need to be saving it every other minute as that’ll just cause network lag.
This is probably the main reason you are loosing data. I haven’t had a data lose problem only doing when the data is changed and when a player leaves.
Sample code from Ultimate Boxing of how I implemented it:
game:BindToClose(function()
for _,Player in pairs(game.Players:GetPlayers()) do
spawn(function() SavePlayer(Player) end)
end
while true do
local SaveDone = true
for _,Value in pairs(LastDataSaved) do
if Value == false then
SaveDone = false
end
end
if SaveDone == true then break end
wait()
end
end)
In a nutshell, here is what it does:
- Flushes all the players. You should add in something that prevents re-saving that hasn’t changed to prevent it taking more time.
- In a loop, waits for all the players to be done saving.
- Close.