Data loss (DataStore)

Hello! I’m trying to make a DataStore to save a numbervalue in my game, however
I saw in public server with 10/12 players, the output send a lot of "DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = (user id I think)

I tried to use someone’s datastore that works and adapt it with my datastore, it saves sometimes, but doesn’t save sometimes, I even tried to look at other people forums

Here’s the server script :

function SafeSave(Datastore, Key, Value)
	local Attempts = 0
	local Success = false
	local Error = nil
	repeat
		Attempts += 1
		Success, Error = pcall(function()
			Datastore:SetAsync(Key, Value)
		end)
		if not Success then task.wait(10) end
	until
	Success or Attempts >= 3
	if Error ~= nil or not Success then
		print("<Error while saving data>\n    - "..(Error or "No Error")) 
		return nil 
	end
	print("Save success (Punches : "..Value..")")
	return Success
end


function SafeGet(Datastore, Key)
	local Attempts = 0
	local Data = nil
	local Success = false
	local Error = nil
	repeat
		Attempts += 1
		Success, Error = pcall(function()
			Data = Datastore:GetAsync(Key)
		end)
		if not Success then wait(10) end
	until
	Success or Attempts >= 3
	if Error ~= nil or not Success then print("<Error while retrieving data>\n    - "..(Error or "No Error")) return nil end
	return Data
end


function SafeUpdate(Datastore,Key,Punches)
	local Attempts = 0
	local Data = nil
	local Success = false
	local Error = nil
	repeat
		Attempts += 1
		Success, Error = pcall(function()
			Data = Datastore:UpdateAsnyc(Key,Punches)
		end)
		if not Success then wait(10) end
	until
	Success or Attempts >= 3
	if Error ~= nil or not Success then
		print("<Error while updating data>\n    - "..(Error or "No Error"))
		return nil
	end
	return Data
end


local PunchesDataStore = game:GetService("DataStoreService"):GetDataStore("Punches")
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Punches = Instance.new("NumberValue")
	Punches.Name = "Punches"
	Punches.Parent = leaderstats

	local Glove = Instance.new("StringValue")
	Glove.Name = "Glove"
	Glove.Value = "Killstreak"
	Glove.Parent = leaderstats

	print("Joined")
	Punches.Value = SafeGet(PunchesDataStore,player.UserId) or 0
end)


game.Players.PlayerRemoving:Connect(function(player)
	SafeSave(PunchesDataStore,player.UserId,player.leaderstats.Punches.Value)
end)

Thank you if you can help! Sorry if I wasn’t clear enough

Try looking into this amazing module called ProfileService - It prevents all this and is easy to use.

I forgot to do task.wait(10) for the SafeGet function, dummy me

1 Like

Hey sorry I found the issue thanks anyways!

DataStore has a 6 seconds cooldown between write requests for the same key:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.