DataStore request was added to queue (lost all data) tool saver

DataStore request was added to queue. (lost all data)

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Dvddlayer")

local function saveDate(player)
	local tableToSave = {
		player.leaderstats.Wins.Value;
		--player.Stages.Value;
		player.TimePlayed.Value
	}
	
	local success, err = pcall(function()
		datastore:SetAsync(player.UserId, tableToSave)
	end)
	
	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!!")
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
	wins.Value = 0
	
	--local Stage = Instance.new("IntValue")
	--Stage.Name = "Stages"
	--Stage.Parent = player
	--Stage.Value = 0
	
	local TimePlayed = Instance.new("IntValue")
	TimePlayed.Name = "TimePlayed"
	TimePlayed.Parent = player
	TimePlayed.Value = 0


local data
local success, err = pcall(function()
	
		data = datastore:GetAsync(player.UserId)
		
	end)
	
	if success and data then
		
		wins.Value = data[1]
		--Stage.Value = data[2]
		TimePlayed.Value = data[2]
		
	else
		print("The player has no data")
	end
	
	game.Players.PlayerRemoving:Connect(function(player)
		local success, err = pcall(function()
			saveDate(player)
		end)
		
		if success then
			print("data has been saved")
		else
			print("data has not been saved!")
		end
	end)
	
		for _, player in pairs(game.Players:GetPlayers()) do 
			local success, err  = pcall(function()
				saveDate(player) 
			end)

			if success then
				print("Data has been saved")
			else
				print("Data has not been saved!")
			end
			end
		end)
local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedTools")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SieDffsagtfa32")

game.Players.PlayerAdded:Connect(function(Player)
	local ToolData = SaveData:GetAsync(Player.UserId)
	
	local BackPack = Player:WaitForChild("Backpack")
	local StarterGear = Player:WaitForChild("StarterGear")
	
	if ToolData ~= nil then
		for i, v in pairs(ToolData) do
			if ToolFolder:FindFirstChild(v) and BackPack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
				ToolFolder[v]:Clone().Parent = BackPack
				ToolFolder[v]:Clone().Parent = StarterGear
			end
		end
	end

	Player.CharacterRemoving:Connect(function(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local ToolTable = {}
	
	for i, v in pairs(Player.Backpack:GetChildren()) do
		table.insert(ToolTable, v.Name)
	end
	if ToolTable ~= nil then
		SaveData:SetAsync(Player.UserId, ToolTable)

	end
end)

I have problem with datastore all the time can somebody help me with that.


Its show a lots of that in output

The code is all messed up; I can’t really understand it. But it seems that you are creating an event for leaving the game and looping the players every time they join the server. You need to only have one event for leaving the game, so you need to put that outside the PlayerAdded function. Also, why do you need to loop the players and save their data every time someone joins? I assumed this was the case seeing as the end of the PlayerAdded function was at the very end.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	...
end)

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

so I should do it like that?

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Dvddlayer")
local Players = game:GetService("Players")

local function saveDate(player)
	local tableToSave = {
		player.leaderstats.Wins.Value;
		--player.Stages.Value;
		player.TimePlayed.Value
	}
	
	local success, err = pcall(function()
		datastore:SetAsync(player.UserId, tableToSave)
	end)
	
	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!!")
		warn(err)
	end
end

Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
	wins.Value = 0
	
	--local Stage = Instance.new("IntValue")
	--Stage.Name = "Stages"
	--Stage.Parent = player
	--Stage.Value = 0
	
	local TimePlayed = Instance.new("IntValue")
	TimePlayed.Name = "TimePlayed"
	TimePlayed.Parent = player
	TimePlayed.Value = 0


local data
local success, err = pcall(function()
	
		data = datastore:GetAsync(player.UserId)
		
	end)
	
	if success and data then
		
		wins.Value = data[1]
		--Stage.Value = data[2]
		TimePlayed.Value = data[2]
		
	else
		print("The player has no data")
	end
	
	Players.PlayerRemoving:Connect(function(player)
		local success, err = pcall(function()
			saveDate(player)
		end)
		
		if success then
			print("data has been saved")
		else
			print("data has not been saved!")
		end
	end)
	
		for _, player in pairs(game.Players:GetPlayers()) do 
			local success, err  = pcall(function()
				saveDate(player) 
			end)

			if success then
				print("Data has been saved")
			else
				print("Data has not been saved!")
			end
			end
		end)

.

I still don’t see any difference; you just added the local Players; the PlayerRemoving function is still inside the PlayerAdded function; and just remove the for loop at the very end; it’s unneccessary.

I think I did what you said me to do, and if not you can show me how?

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("Dvddlayer")
local Players = game:GetService("Players")

local function saveDate(player)
	local tableToSave = {
		player.leaderstats.Wins.Value;
		player.TimePlayed.Value
	}
	
	datastore:SetAsync(player.UserId, tableToSave)
end

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
	wins.Value = 0
	
	local TimePlayed = Instance.new("IntValue")
	TimePlayed.Name = "TimePlayed"
	TimePlayed.Parent = player
	TimePlayed.Value = 0

	local success, data = pcall(function()
		return datastore:GetAsync(player.UserId)
	end)
	
	if success and data then
		wins.Value = data[1]
		TimePlayed.Value = data[2]
	end
end)

Players.PlayerRemoving:Connect(function(player)
	pcall(saveDate, player)
end)

you sure its ok if the player leave too fast or that sh*t problems any won’t be wrong?

You don’t get it, do you? The event will get fired anytime any player leaves the game.

I get it but I did it in the past and something went wrong

but whatever what to do about the tool saver?