How to prevent datastore queues when saves need to made everytime a message is sent

I get warnings of data store queue filling up for specific keys, and then after a while it says it errors

I’ve read the documentation, and there 2 players in the server so to my understanding

numPlayers = 2
60 + numPlayers × 10

So to my understanding, that means I can have 80 Set requests a minute. There was no way I was exceeding that, and can see in the SS that the console doesn’t have 80 requests (probably only around 40 or so), yet it still throws warnings at the bottom saying data isn’t saving.

Is there anyway to optimize this, as 40 requests in a short period of time seems pre bad. I added a debounce on the client (this function below is fired on the server when a player clicks a button) and that debounce will hopefully prevent spam clicking, but I know exploiters can spam the remote event anyway, however, they wouldn’t be able to get far unless they entered a User that is in their friends list name.

local function MessageSent(player, sendingTo, message)
	-- Get player sending to
	local RecipientID
	local Success, Error = pcall(function()
		RecipientID = Players:GetUserIdFromNameAsync(sendingTo)
	if not Success then
		return 'Player does not exist'
	if not RecipientID then return 'Error' end
	-- Make sure player is friends with RecipientID (can only send messages to friends)
	if not player:IsFriendsWith(RecipientID) then return 'Error' end
	local FilteredMessage = Chat:FilterStringForBroadcast(message, player)
	local Data = GetData(RecipientID)
	if Data then
		table.insert(Data['Messages'], FilteredMessage)
		local TryCount = 0
		local Success, Error
		-- Save data
			Success, Error = pcall(function()
				DataStore:SetAsync(RecipientID, Data)
			TryCount = TryCount + 1
		until TryCount >= Tries or Success
		-- Failed to save
		if not Success then
			warn('Data failed to save | Error Code:' .. tostring(Error))
			return 'Error'
		if Success then
			MessagingService:PublishAsync('Messages', {Message = FilteredMessage, Sending = player.Name, Recipient = sendingTo})
			return 'Sent!'

I can’t do any saving when player leaves with this either, as this is made to change player data whether or not they are in the server

That error occurs when you use data from the same key within 6 seconds the first request. Now if you want to add in a debounce on the server I believe a system like the leaky bucket system will help you

But why do you need to instantly store these messages instantly? Why don’t you just pack them all up and then save them every few minutes.

Hmm ok.

As for putting them all somewhere, then saving every few minutes, I would, if I knew how XD

Just as a message into a table. If you’re worried about the player leaving the server and not storing the data, if you have a table dedicated for each player then you would be able to store the UserId in the first index and add the rest of the message. Then say after 30 seconds you store the data you just start at index 2 and make the recipientid index 1 (the userid). I don’t know if it makes sense. It’s 3:37 and I’m tired. Hopefully it helps, but if you store the messages to a userid in a table then it could help solve the storing of data and the leaving server problems.