Why is it that I get a 'Data Store Added to Queue' warning so early?

Hi! So I have this minor issue. A ‘data store added to queue’ warning pops up the second I test out the game.

Script:

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Coins.Value;
		player.leaderstats.Wins.Value;
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data has not been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	
	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 0
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	Wins.Value = 0

	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
	end)

	if success and data then -- If there were no errors and player loaded the data

		Coins.Value = data[1] -- Set the coins to the first value of the table (data)
		Wins.Value = data[2] -- Set the wins to the second value of the table (data)
		
	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

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

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

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

Anyone willing to help me with this?

1 Like

Those warnings will appear if you attempt to set the value of a DataStore’s key twice within a timespan of 6 seconds. It should be noted that a DataStore’s key’s value can be set 10 times per minute. The warning is likely occurring due to the PlayerRemoving and BindToClose functions.

3 Likes

But the error is occuring right as he joins.

The PlayerRemoving and BindToClose won’t error seeing as if the player is already removed then the BindToClose won’t affect them. IF they are still in the server (meaning probably studio) then only the first one started will run.

It has to be some other script that is also calling GetAsync on that same key.

1 Like

It’s not an error just a warning. Still, thanks for letting me know about it.

1 Like

I wasn’t able to check the video since it didn’t load, I assumed that the issue was occuring when they left the server, not joined (as that is the usual issue).

The PlayerRemoving and BindToClose won’t error

You appear to be conflating a ‘warning’ with an ‘error’. A warning, is just that, a warning (it doesn’t terminate the function call stack), an error, on the other hand, does terminate the function call stack.

IF they are still in the server (meaning probably studio) then only the first one started will run.

This is incorrect, in studio both the BindToClose() callback and the PlayerRemoving event are invoked and fired respectively. This is a common source for the warnings that the thread’s poster is receiving.

2 Likes

Sorry I ment to say warning thank you for correcting me.

1 Like

Sorry I thought only one would run. I have a datastore that has to use BindToClose in studio and doesn’t warn even though it also has PlayerRemoving. Thank you for correcting me.