I keep getting "DataStore request was added to queue" eventhough I'm literally sending 1 request on leave

This script works on another game of mine but with different values. I’m just trying to make the options persistent but I can’t. I keep getting this warning and my data cannot be saved:
DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests

local dss = game:GetService("DataStoreService")
local ps = game:GetService("Players")
local http = game:GetService("HttpService")
local rs = game:GetService("ReplicatedStorage")
local settingsEvent = rs:WaitForChild("Settings")

playerClasses = require(rs:WaitForChild("playerClasses"))

local onJoinTable = {
	[true] = function(player, result)
		print("no data was found, player joined for the first time, giving welcome badge.")
		local playerClass = require(script.PlayerClass).new(player)
		playerClasses[ps:GetUserIdFromNameAsync(player.Name)] = playerClass
		print(playerClass)
	end,
	[false] = function(player, result)
		print("Player data was found, loading data.")
		local playerClass = require(script.PlayerClass).new(player, http:JSONDecode(result))
		playerClass:setName(player.Name)
		playerClass:setCharacter(player.Character)
		
		playerClasses[ps:GetUserIdFromNameAsync(player.Name)] = playerClass
		print(playerClass)
	end,
}

local playerClassDB = dss:GetDataStore("_playerClassData")

-- FUNCTIONS --

function joinHandler(player)
	local playerId = ps:GetUserIdFromNameAsync(player.Name)
	
	local success, result = pcall(function()
		return playerClassDB:GetAsync(playerId)
	end)
	if not success then
		warn("Data was not loaded.")
		player:Kick("couldnt load data, try joining again and if the issue persists then contact gk983")
	end
	onJoinTable[result == nil or result == "null"](player, result)
end

function save(playerId, playerClass)
	local success, err = pcall(function()
		local jsonStructure = http:JSONEncode(playerClass)
		playerClassDB:SetAsync(playerId, jsonStructure)
	end)
end

function leaveHandler(player)
	--if not game:GetService("RunService"):IsStudio() then
		local playerId = ps:GetUserIdFromNameAsync(player.Name)
		save(playerId, playerClasses[playerId])
	--else
	--	print("Cancelled data save. Player is in studio.")
	--end
end

-- EVENTS --

ps.PlayerAdded:Connect(joinHandler)

ps.PlayerRemoving:Connect(leaveHandler)

game:BindToClose(function()
	--if not game:GetService("RunService"):IsStudio() then
		print("Server shut down.")
		require(script.ForLoops).pairsForLoop(save, playerClasses)
	--else
	--	print("Cancelled data save. Player is in studio.")
	--end
end)

settingsEvent.OnServerEvent:Connect(function(player, setting, value)
	
	local pc = require(script.PlayerClass)
	if setting == "music" then
		pc:setMusic(value)
		return
	end

	if setting == "subtitles" then
		pc:setSubtitles(value)
		return
	end

	if setting == "shadows" then
		pc:setShadows(value)
		return
	end
end)

playerclass

local playerClass = {}
playerClass.__index = playerClass

function playerClass.new(player, self)
	repeat task.wait() until player.Character ~= nil
	
	if self then return setmetatable(self, playerClass) end
	local self = setmetatable({}, playerClass)
	self.name = player.Name
	self.character = player.Character
	self.music = true
	self.shadows = true
	self.subtitles = true
	return self
end

-- ID STUFF

function playerClass:setCharacter(character)
	self.character = character
end

function playerClass:setName(name)
	self.name = name
end

-- Settings

function playerClass:setMusic(music)
	self.music = music
end

function playerClass:getMusic()
	return self.music
end

function playerClass:setSubtitles(subtitles)
	self.subtitles = subtitles
end

function playerClass:getSubtitles()
	return self.subtitles
end

function playerClass:setShadows(shadows)
	self.shadows = shadows
end

function playerClass:getShadows()
	return self.shadows
end

return playerClass
1 Like

From what I see, it is more than likely the BindToClose function. Since when playing in studio, or if you’re the only person in the server, it will be saving data for that player twice, since they’re leaving, and the game is shutting down as a result of them leaving, thus triggering the request queue message because the game is saving data for a certain player more than once in less than a second (I could be wrong about it being 1 second. It may be half a second, or even a .1 second time-frame). Hope this helps!

3 Likes

this seems like the reason. thank you so much, I have no idea how I didn’t notice.

1 Like

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