Saving settings in DataStore2

I was making a settings gui, and I was thinking how to save the values.
Obviously, datastore2 works like this:
Save data to datastore > value object updates

However, I cant really do that in this case, since you can easily spam the settings, potentially breaking the datastore.

How could I save multiple values like this efficently?

For the spamming settings you could just let them save their settings every so often, all ya need is a debounce

Also for the saving portion you could just use a dictionary because with Datastore2 you can effectively save a whole dictionary

_G.Data = {}



game.Players.PlayerAdded:Connect(function(player)
	_G.Data[player.UserId] = {
		
		["Settings"] = {
			
			["Music"] = false;
			["Theme"] = "Dark";
			["Sounds"] = false;
			
		}
		
	}

end)

Get and Save

_G.Data = {}
local dataStore2 = require(1936396537)




game.Players.PlayerAdded:Connect(function(player)
	
	local function setupUserData()
		_G.Data[player.UserId] = {
			
			["Settings"] = {

				["Music"] = false;
				["Theme"] = "Dark";
				["Sounds"] = false;

			}	
			
		}
	end
	local userData = dataStore2("Data", player):Get(setupUserData()) -- get
	dataStore2("Data", player):Set(userData) -- save	
end)

DataStore2, if I remember correctly, doesn’t need any value objects to function.

1 Like

I know, I just have them so the client and server can see them

I cant just save the data every time a setting changes because it can be spammed though

So shouldn’t you save on PlayerRemoving?

Late response, but I tried this and it does not work.
I assume its because datastore2 needs a player object to get the datastore, which doesnt exist after leaving