Datastore not working probably

So i made this script to save my values and it dosen’t work probably

Output/Results
image
So if I gave the values another name they will just keep there normal names so the datastore doesn’t even save

Also if you want to work with the script directly here is it

local Data = game:GetService(“DataStoreService”):GetDataStore("–Data–")

game.Players.PlayerAdded:Connect(function(Player)
local Key = “Data”…Player.UserId
local Folder = Instance.new(“Folder”,Player)
Folder.Name = “Data”
local Values = Instance.new(“Folder”,Folder)
Values.Name = “Values”
local Save = Data:GetAsync(Key)
if Save then
for i = 1,#Save do
local NumberValue = Instance.new(“NumberValue”,Values)
NumberValue.Name = Save[i]
end
else
print(“This player has not saved any data yet”)
local Backpack = Instance.new(“NumberValue”,Values)
Backpack.Name = “Backpack”
local CoinHandler = Instance.new(“NumberValue”,Values)
CoinHandler.Name = “CoinHandler”
local CoinValue = Instance.new(“NumberValue”,Values)
CoinValue.Name = “CoinValue”
local BPriceHandler = Instance.new(“NumberValue”,Values)
BPriceHandler.Name = “BPriceHandler”
local CPriceHandler = Instance.new(“NumberValue”,Values)
CPriceHandler.Name = “CPriceHandler”
end
end)

game.Players.PlayerRemoving:Connect(function(Player)
local Key = “Data”…Player.UserId
local Values = {}
for i,v in pairs(Player.Data.Values:GetChildren()) do
if v:isA(“NumberValue”) then
table.insert(Values,v.Name)
end
Data:SetAsync(Key, Values)
end
end)

1 Like

I highly recommend that you do not use value object for storing data as it is slower and more complex to save / load.

You issue is that you are not storing the value. You are only storing the name.

I would swap to using a dictionary over an array as you can then do

Values[v.Name] = v.Value

I am sure you can handle the loading process from here.

You should also look at saving data on close. DataModel | Documentation - Roblox Creator Hub

2 Likes

Thanks alot i’m going to try that out right away

1 Like

Also what would you recommend me to save my data in instead of NumberValues

1 Like

Something along the lines of this.

Though I would out it inside a module excluding the events so that other parts of your game can access the player data when needed. e.g. plrDataList[plr.UserId] data. You might also want to use OOP for setting and getting player data correctly.

1 Like

Okay, but my script isn’t really working

local Data = game:GetService(“DataStoreService”):GetDataStore(“Data”)

game.Players.PlayerAdded:Connect(function(Player)
local Key = “Data”…Player.UserId
local Folder = Instance.new(“Folder”,Player)
Folder.Name = “Data”
local Values = Instance.new(“Folder”,Folder)
Values.Name = “Values”
local Save = Data:GetAsync(Key)
if Save then
for i = 1,#Save do
print(i)
local NumberValue = Instance.new(“NumberValue”,Values)
NumberValue.Name = Save[i]
end
else
print(“This player has not saved any data yet”)
local Backpack = Instance.new(“NumberValue”,Values)
Backpack.Name = “Backpack”
local CoinHandler = Instance.new(“NumberValue”,Values)
CoinHandler.Name = “CoinHandler”
local CoinValue = Instance.new(“NumberValue”,Values)
CoinValue.Name = “CoinValue”
local BPriceHandler = Instance.new(“NumberValue”,Values)
BPriceHandler.Name = “BPriceHandler”
local CPriceHandler = Instance.new(“NumberValue”,Values)
CPriceHandler.Name = “CPriceHandler”
end
end)

game.Players.PlayerRemoving:Connect(function(Player)
local Key = “Data”…Player.UserId
local Values = {}
for i,v in pairs(Player.Data.Values:GetChildren()) do
if v:isA(“NumberValue”) then
Values[v.Name] = v.Value
end
if i == #Player.Data.Values:GetChildren() then
print(“Saving”)
Data:SetAsync(Key, Values)
end
end
end)

1 Like

Update how you load the data in.

1 Like

It’s just saying that i’m sending to many request to the datastore "DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = Data100053340 (x4)
"

1 Like

I have tried really hard now but i just can’t get this to work perhaps i can get some more tips

1 Like

Nevermind i fixed it ima just give you a solution

I’m not sure what would be causing you to be bumping into the rate limits (you can read more about the limitations here), but one thing I did note is that when loading player data, you are not setting the values of the NumberValues you create.

Here is your script with a few edits:

  • Set the value of each numberValue created
  • BindToClose() added so that if the server shuts down while players are playing, their data has less chance of being lost
  • Formatted (indentations, newlines, and capitalization)
  • Some extrapolations made to avoid repeated code (Players service, getKey)
  • Parent the dataFolder after all the data has been parsed
  • for loops changed to foreach style loops for easier readability
  • Warning and cancellation if, for some reason, the data folder was not found while saving data
local Players = game:GetService("Players")
local dataStore = game:GetService("DataStoreService"):GetDataStore("Data")

local function getKey(player)
	return "Data" .. tostring(player.UserId)
end

Players.PlayerAdded:Connect(function(player)
	local dataFolder = Instance.new("Folder")
	dataFolder.Name = "Data"

	local valuesFolder = Instance.new("Folder", dataFolder)
	valuesFolder.Name = "Values"

	local loadedData = dataStore:GetAsync(getKey(player))
	if loadedData then
		for key, value in pairs(loadedData) do
			local numberValue = Instance.new("NumberValue", valuesFolder)
			numberValue.Name = key
			numberValue.Value = value
		end
	else
		local backpack = Instance.new("NumberValue", valuesFolder)
		backpack.Name = "Backpack"

		local coinHandler = Instance.new("NumberValue", valuesFolder)
		coinHandler.Name = "CoinHandler"

		local coinValue = Instance.new("NumberValue", valuesFolder)
		coinValue.Name = "CoinValue"

		local bPriceHandler = Instance.new("NumberValue", valuesFolder)
		bPriceHandler.Name = "BPriceHandler"

		local cPriceHandler = Instance.new("NumberValue", valuesFolder)
		cPriceHandler.Name = "CPriceHandler"
	end

	dataFolder.Parent = player
end)

local function savePlayerData(player)
	local values = {}

	local valuesFolder = player:FindFirstChild("Data") and player.Data:FindFirstChild("Values")

	if not valuesFolder then
		warn("Player left, but no values folder was found.")
		return
	end

	for _, numberValue in pairs(valuesFolder:GetChildren()) do
		if numberValue:IsA("NumberValue") then
			values[numberValue.Name] = numberValue.Value
		end
	end

	dataStore:SetAsync(getKey(player), values)
end

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		savePlayerData(player)
	end
end)
Players.PlayerRemoving:Connect(savePlayerData)
1 Like