I'm not great with datastores, but I've been having an issue with my game saving. Whenever I add a value to myself and leave I get a warning: DataStore request was added to queue. If request queue fills, further requests will be dropped. Here is my script:
local DataStoreService = game:GetService("DataStoreService") local PlayerData = DataStoreService:GetDataStore("General") game.Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(char) MakeStats(player) end) wait(0.5) print("e") LoadData(player) end) game.Players.PlayerRemoving:Connect(function(player) SaveData(player) end) function MakeStats(player) if player:FindFirstChild("playerstats") == nil then local PlayerFolder = Instance.new("Folder",player) PlayerFolder.Name = "playerstats" local PlayerCash = Instance.new("NumberValue",PlayerFolder) PlayerCash.Name = "Cash" local PlayerBxp = Instance.new("NumberValue",PlayerFolder) PlayerBxp.Name = "BadXP" local PlayerGxp = Instance.new("NumberValue",PlayerFolder) PlayerGxp.Name = "GoodXP" end end function LoadData(player) local cashdata = PlayerData:GetAsync(player.UserId,"cash") local badxp = PlayerData:GetAsync(player.UserId,"badxp") local goodxp = PlayerData:GetAsync(player.UserId,"goodxp") local PlayerFolder = player:WaitForChild("playerstats") local PlayerCash = PlayerFolder.Cash local PlayerBxp = PlayerFolder.BadXP local PlayerGxp = PlayerFolder.GoodXP PlayerCash.Value = cashdata PlayerBxp.Value = badxp PlayerGxp.Value = goodxp end function SaveData(player) local PlayerFolder = player:WaitForChild("playerstats") local PlayerCash = PlayerFolder.Cash local PlayerBxp = PlayerFolder.BadXP local PlayerGxp = PlayerFolder.GoodXP PlayerData:SetAsync(player.UserId,"cash",PlayerCash.Value) PlayerData:SetAsync(player.UserId,"badxp",PlayerBxp.Value) PlayerData:SetAsync(player.UserId,"goodxp",PlayerGxp.Value) end
I don't see what I'm doing wrong, but if anyone could help that would be appreciated! Thanks!
You are using data stores incorrectly which is why you are getting a warning. In short you are saving data to the same key too fast (6 within 6 seconds). This also means when you are savind data you are overwriting it as it is the same key.
Secondly SetAsync accepts two paramater the first is the string key and the second is the value to set. GetAsync accepts only one paramater which is the key to get the data.
A data store key has a large character limit and to save on requests you can use one key in most scenarios.
You can pass a table directly as the save value for a key but with limitations. It must be compatible with JSON encoding which means you can only store primitive types (string, numbers, boolean) and not be a mixed table. You can check if you have a valid format by using the JSON encoding function in the HTTPService (you do not need to enable HTTP to use the function).
You can save the encoded string and decode it when you use GetAsync but Roblox will handle this for you if you save a table directly.
Example
local DataStoreService = game:GetService("DataStoreService") local PlayerData = DataStoreService:GetDataStore("General") local keyPrefix = 'usr_' -- using a prefix can help organise keys a bit local plrData = {} -- holds player data while ingame game.Players.PlayerAdded:Connect(function(player) local data = PlayerData:GetAsync(tostring(player.UserId)) print('Player data') if data then data.JoinCount = data.JoinCount +1 plrData[player.UserId] = data for i, v in pairs(data) do print(i, v) end else print('Player has no data') plrData[player.UserId] = { Points = 100, JoinCount = 1 } end end) game.Players.PlayerRemoving:Connect(function(player) PlayerData:SetAsync(tostring(player.UserId), plrData[player.UserId]) plrData[player.UserId] = nil -- clean up data end)
This is just a example and is not suited for a live game as it does not included any error handling. Please check the wiki on how to handle data store errors.
I hope this helps. Please comment if you have any other questions.