I have a SetAsync running with the onPlayerRemoved thingy but it is not saving upon exiting, at least not saving correctly, so I think I need to move to something that will update/set the datastore every time one of the values are changed. However I need help with how to use such a thing. Do I need to make a Change:connect(function() for each IntValue and BoolValue or can they all be under one?

Here is my script that DOES NOT SAVE correctly when leaving. When I come back, the Coins on leaderstats are different/bigger and the Int/Bool values are not saved at all.


local DataStore = game:GetService("DataStoreService"):GetDataStore("supahdupahstore")

game.Players.PlayerRemoving:connect(function(player)
	local key = "player-"..player.userId
	local valuesToSave = {player.leaderstats.Coins.Value, player.leaderstats.XP.Value, player.hp.Value, player.s.Value, player.j.Value, player.bw.Value, player.i.Value, player.f.Value, player.l.Value, player.b.Value, player.se.Value, player.sw.Value}	
	DataStore:SetAsync(key, valuesToSave)	
	print("Saved everything to the store")
end)

My script for the Change:connect(function()

player.leaderstats.Coins.Changed:connect(function()
print("Saving to data store...")	
DataStore:SetAsync(key, valuesToSave)	
wait()
print("Saved to data store")
end)

Now I realize this is an issue because if I need to make a Changed:connect(function() for each value....I am going to be calling SetAsync too many times, what can I do?

1 comment

0

Can you include the loading of the users data? It is not a good idea to save the data every time it changes ar you will quickly use all of the request limits.