How can I avoid this?

Hello! My datastore isn’t working, Im trying to save players data but then when I only have 2 players, the whole thing breaks and no ones data is saved, nor when they leave or rejoin the game.

Here is the error

 14:47:48.803  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -1  -  Studio
  14:47:49.082  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -2  -  Studio
  14:47:49.275  Player1 is still in the game!  -  Server - MainScript:117
  14:47:50.277  Player2 is still in the game!  -  Server - MainScript:117
  14:47:52.284  Player1 is still in the game!  -  Server - MainScript:117
  14:47:53.296  Player2 is still in the game!  -  Server - MainScript:117
  14:47:53.924  Tagged  -  Server - Rocket:79
  14:47:54.937  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -2  -  Studio
  14:47:55.314  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -1  -  Studio
  14:47:55.361  Player1 is still in the game!  -  Server - MainScript:117
  14:47:56.375  Player2 is still in the game!  -  Server - MainScript:117
  14:47:58.491  Player1 is still in the game!  -  Server - MainScript:117
  14:47:59.527  Player2 is still in the game!  -  Server - MainScript:117
  14:48:00.872  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -2  -  Studio
  14:48:01.175  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -1  -  Studio
  14:48:01.308  Tagged  -  Server - Rocket:79
  14:48:01.662  Player1 is still in the game!  -  Server - MainScript:117
  14:48:02.668  Player2 is still in the game!  -  Server - MainScript:117
  14:48:03.712  Player1Has been removed!  -  Server - MainScript:121
  14:48:03.716  End of game!  -  Server - MainScript:152
  14:48:07.420   ▶ DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -2 (x2)  -  Studio
  14:48:14.202  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -1  -  Studio
  14:48:19.859  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -2  -  Studio
  14:48:20.112  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -1  -  Studio
  14:48:26.867  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -2  -  Studio
  14:48:27.380  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -1  -  Studio
  14:48:33.778  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = -2  -  Studio

Here is the Stats Script

local dataStores = game:GetService("DataStoreService")
local data = dataStores:GetDataStore("TixDataStore")
local defaultTix = 0
local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)
	
	playersLeft = playersLeft + 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Tix = Instance.new("IntValue")
	Tix.Name = "Tix"
	Tix.Value = 0
	Tix.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Value = 0
	Wins.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		
		character.Humanoid.Died:Connect(function()
			-- Whenever sombody dies, this event will run
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end
			
			player:LoadCharacter()
		end)
		
	end)
	
	local wns,tx
	local success,nope = pcall(function()
                wns = data:GetAsync("Wins_"..player.UserId)
		tx = data:GetAsync("Tix_"..player.UserId)
	end)

	if (success and not nope) then
		Tix.Value = tx
		wns.Value = wns
	elseif (nope and not success) then
		error(nope)
	end

	game.Players.PlayerRemoving:Connect(function(player)

		local id = player.UserId
		local ls = player.leaderstats

		local yes,no = pcall(function()

			data:SetAsync("Tix_"..id, ls.Tix.Value)
                        data:SetAsync("Wins_"..id, ls.Wins.Value)

		end)

		if (yes and not no) then
			print("Data saved for " .. player.Name)
		elseif (no and not yes) then
			error(no)
		end

	end)
end)
2 Likes
local dataStores = game:GetService("DataStoreService")
local data = dataStores:GetDataStore("TixDataStore")
local defaultTix = 0
local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)
	
	playersLeft = playersLeft + 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Tix = Instance.new("IntValue")
	Tix.Name = "Tix"
	Tix.Value = 0
	Tix.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Value = 0
	Wins.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		
		character.Humanoid.Died:Connect(function()
			-- Whenever sombody dies, this event will run
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end
			
			player:LoadCharacter()
		end)
		
	end)
	
	local wns,tx
	local success,nope = pcall(function()
                wns = data:GetAsync("Wins_"..player.UserId)
		tx = data:GetAsync("Tix_"..player.UserId)
	end)

	if (success and not nope) then
		Tix.Value = tx
		wns.Value = wns
	elseif (nope and not success) then
		error(nope)
	end
end)

	game.Players.PlayerRemoving:Connect(function(player)

		local id = player.UserId
		local ls = player.leaderstats

		local yes,no = pcall(function()

			data:SetAsync("Tix_"..id, ls.Tix.Value)
                        data:SetAsync("Wins_"..id, ls.Wins.Value)

		end)

		if (yes and not no) then
			print("Data saved for " .. player.Name)
		elseif (no and not yes) then
			error(no)
		end

	end)

you put the playerremoving inside of the playeradded, which would connect to removing players a lot

my apologies for the messed up formatting

3 Likes

I dont understand


1 Like

What @Unphases is implying from his perspective (which is correct), is that you have created a function within a function. In general terms garbage collector would clean this up, but with an event function this will continue to be cloned every time the player joins.
e.g

bad:

game.Players.PlayerAdded:Connect(function()
game.Players.PlayerRemoving:Connect(function()
end) -- this is always creating a player removed function every time a player joins.
end)

good:

game.Players.PlayerAdded:Connect(function()
end)
game.Players.PlayerRemoving:Connect(function()
end)
2 Likes

what line is this on?


1 Like