Um, is this bad? (Datastores)

I have made a data saving script which currently saves 4 values. Everytime it saves, I mean it works, but it says, DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = Coins_182729712

So my question is, even though the data properly saves, is it bad to save so many values? Is it bad to leave it how it is?

Here is the script:

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local stamina = Instance.new("IntValue")
	stamina.Name = "Stamina"
	stamina.Value = 100
	stamina.Parent = player
	
	local isSprinting = Instance.new("BoolValue")
	isSprinting.Name = "IsSprinting"
	isSprinting.Parent = player
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Coins"
	cash.Parent = leaderstats
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats
	
	local experience = Instance.new("IntValue")
	experience.Name = "Experience"
	experience.Parent = player
	
	local neededXP = Instance.new("IntValue")
	neededXP.Name = "NeededXP"
	neededXP.Parent = player	
	
	local coinsKey = "Coins_"..player.UserId
	local levelKey = "Level_"..player.UserId	
	local XPKey = "XP_"..player.UserId
	local XPNeededKey = "XPNeeded_"..player.UserId
	
	
	local coinsData
	local levelData
	local experienceData
	local neededXPData
	
	local success, errormessage = pcall(function()
		coinsData = myDataStore:GetAsync(coinsKey)
		levelData = myDataStore:GetAsync(levelKey)
		experienceData = myDataStore:GetAsync(XPKey)
		neededXPData = myDataStore:GetAsync(XPNeededKey)
	end)
	
	if success then
		level.Value = levelData
		cash.Value = coinsData
		experience.Value = experienceData
		neededXP.Value = neededXPData
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local coinsKey = "Coins_"..player.UserId
	local levelKey = "Level_"..player.UserId	
	local XPKey = "XP_"..player.UserId
	local XPNeededKey = "XPNeeded_"..player.UserId
	
	local coinsData = player.leaderstats.Coins.Value
	local levelData = player.leaderstats.Level.Value
	local XPData = player.Experience.Value
	local XPNeededData = player.NeededXP.Value

	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(coinsKey, coinsData)
		myDataStore:SetAsync(levelKey, levelData)
		myDataStore:SetAsync(XPKey, XPData)
		myDataStore:SetAsync(XPNeededKey, XPNeededData)
	end)
	
	if success then
		warn("Data was saved!")
	else
		warn(errormessage)
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local coinsKey = "Coins_"..player.UserId
		local levelKey = "Level_"..player.UserId	
		local XPKey = "XP_"..player.UserId
		local XPNeededKey = "XPNeeded_"..player.UserId

		local coinsData = player.leaderstats.Coins.Value
		local levelData = player.leaderstats.Level.Value
		local XPData = player.Experience.Value
		local XPNeededData = player.NeededXP.Value


		local success, errormessage = pcall(function()
			myDataStore:SetAsync(coinsKey, coinsData)
			myDataStore:SetAsync(levelKey, levelData)
			myDataStore:SetAsync(XPKey, XPData)
			myDataStore:SetAsync(XPNeededKey, XPNeededData)
		end)

		if success then
			warn("Data was saved!")
		else
			warn(errormessage)
		end
	end
end)

Is there a way to save the values as one value?

This is because you are saving the value in two ways. If you are in the studio, both the PlayerRemoving event with a connected function on it and the BindToClose function will be prompted.

You can simply use RunService if the server had existed through the studio.

if not RunService:IsStudio() then
    -- And the rest of the code
end

You should put at least one of them if you don’t want that error to occur.

2 Likes

yes you can save all the value in a table then save the table in the datastore as this way it will prevent from flooding the datastore

and also you dont need to setasync in the bindtoclose function you can just wait(5) in the bindtoclose function to let the player removing do its job

2 Likes