Datastore not saving values

Hello developers!

I’ve encountered this problem for many days now for my game and I finally decided to find out the reason.

My datastore script either isn’t saving or isn’t loading the data (or both). I have an error snippet which isn’t printing out anything. I have a folder template and I’m looping through all integer, number, string and bool values to save and load the data in a datastore which the name of is the player’s user Id. Here’s the script:

function save(Player)
	
	local DSS = _G.DSS
	
	for _, Value in pairs(game.ServerStorage.PlayerData[Player.UserId]:GetChildren()) do
		
		if Value.ClassName == ('StringValue' or 'IntValue' or 'NumberValue' or 'BoolValue') then
			
			local DataStore = DSS:GetDataStore(tostring(Player.UserId))
		
			local S, E = pcall(function()
			
				DataStore:SetAsync(Value.Name, Value.Value)
			
			end)
		
			if not S then
			
				error("Couldn't save "..Player.Name.."'s data. Error:"..E)
			
			end
				
		end
		
		
		
	end
	
end


function load(Player)

	local DSS = game:GetService("DataStoreService")
	
	local DataFolder = script.Template:Clone()
	DataFolder.Parent = game.ServerStorage.PlayerData
	DataFolder.Name = Player.UserId
	
	for _, Value in pairs(DataFolder:GetDescendants()) do

		if Value.ClassName == ('StringValue' or 'IntValue' or 'NumberValue' or 'BoolValue') then 
			local DataStore = DSS:GetDataStore(tostring(Player.UserId))

			local S, E = pcall(function()

				Value.Value = DataStore:GetAsync(Value.Name)

			end)

			if not S then

				error("Couldn't load "..Player.Name.."'s data. Error: "..E)

			end
			
		end

		

	end
	
	return DataFolder
	
end

game.Players.PlayerAdded:Connect(function(Player)
	
	local Folder = load(Player)
	
	local Events = game.ReplicatedStorage.Events
	Events.UpdateCash:FireClient(Player, Folder.Cash.Value)
	Events.UpdateCrypto:FireClient(Player, Folder.CryptoCoins.Value)
	Events.UpdateTokens:FireClient(Player, Folder.Tokens.Value)
	
	require(script.Earn)(Player)
	
	
end)

game.Players.PlayerRemoving:Connect(save)

Nothing is appearing in output from this script, does anyone know the issue to this? All help is appreciated. Thanks!

1 Like

Is API Service enabled? So that you can use Datastore Service?
image

Also the way you Setting data. Can not be used in the long run. You are using way to many requests for just one player because it’s wrapped within a loop. Say you wanted to have even more data than those 3 values in that case retrieving and storing data would be even more difficult.

This way isn’t efficient or effective each datastore key can store up to 4MB (4,000,000 Characters) your method doesn’t utilize all that space in the datastore. Also keep in mind that…

Each server is allowed a certain number of datastore requests. Having more than one key for a player is fine. But a key for each Value in PlayerData is a bit ineffective seeing that you have to get and set each one and you won’t have enough requests to even do that if you have more PlayerData Values or even players in the Server.

2 Likes

What do I recommend going forward?

Learning how to edit and to store player Data In tables so that you fully utilize space in datastores.

keep in mind the limitations of the Datastore service. and try to follow the best practices of using a datastore.

Best Practices

  • Create Fewer Data Stores
  • Use a Single Object for Related Data
  • Use Key Prefixes to Organize Your Data

Indpeth explanations can be found here - Best Practices

2 Likes

I understand now, thanks for the support!