How to save lots of data without sending too many data store requests?

I’m trying to make a data store to save the settings in my game, I don’t wanna send too many data store requests though. This is the datastore script I tried but it wont save, is there a better way of doing this?

I also get this error message: line 33, attempt to index boolean with 'lasers'

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("Settings")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "settings"
	folder.Parent = player
	
	local lasers = Instance.new("BoolValue")
	lasers.Name = "Lasers"
	lasers.Parent = folder
	
	local muzzleFlash = Instance.new("BoolValue")
	muzzleFlash.Name = "MuzzleFlash"
	muzzleFlash.Parent = folder
	
	local gunLight = Instance.new("BoolValue")
	gunLight.Name = "GunLight"
	gunLight.Parent = folder
	
	local data
	local success, errorMessage = pcall(function()
		data = ds:GetAsync(player.UserId.."-settings") or true
	end)
	
	if success then
		print("Successfully loaded Settings data")
	else
		warn(tostring("Error loading Settings data: "..errorMessage))
	end
	
	if data then
		lasers.Value = data.lasers
		muzzleFlash.Value = data.muzzleFlash
		gunLight.Value = data.gunLight
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data = {
		lasers = player.settings.Lasers.Value;
		muzzleFlash = player.settings.MuzzleFlash.Value;
		gunLight = player.settings.GunLight.Value
	}
	local success, errorMessage = pcall(function()
		ds:SetAsync(player.UserId, data)
	end)
	if success then
		print("Settings data successfully saved")
	else
		warn(tostring("Error saving Settings data: "..errorMessage))
	end
end)

game:BindToClose(function()
	print("Game is closing")
	wait(3)
	print("Game is closed") 
end)

My general method has been to keep a table on the server that contains player data, and then just auto-save that on an interval (every minute or so), and when the player leaves the server. I would highly recommend not sending data every time one little setting changes.

1 Like

I recommend just using Datastore2 , as it is leagues simpler and prevents data loss. I’ve never had issues with data not saving and such using this module.

Also, the reason why you are getting that error on line 33 is because of this line:

data = ds:GetAsync(player.UserId.."-settings") or true

It couldn’t obtain the data, so it just returned true, which is a boolean. Keep in mind that if a player has no saved data in a datastore, it will return nil.

1 Like

Alright I’ll have a look into datastore2