Data store doesnt save data

Hi, I am just working on a data store system that saves Gems and Coins.

But I does not save any data, and I always get the message (after pcall) “data store not saved” (that I wrote).

I do not know why this data store doesnt work, and I would love to get help!

It also thinks that the player never has joined, indicating that the data is not being saved.
Script:

DataStoreService = game:GetService("DataStoreService")
CurrencyData = DataStoreService:GetDataStore("CurrencyData")

game.Players.PlayerAdded:Connect(function(Player)
	
	
	local PlayerData;
	
	
	local CurrencyDataFolder = Instance.new("Folder")
	CurrencyDataFolder.Name = "CurrencyDataFolder"
	CurrencyDataFolder.Parent = Player
	
	local GemsFolder = Instance.new("IntValue")
	GemsFolder.Name = "Gems"
	GemsFolder.Parent = CurrencyDataFolder
	
	local CoinsFolder = Instance.new("IntValue")
	CoinsFolder.Name = "Coins"
	CoinsFolder.Parent = CurrencyDataFolder
	
	
	local DataFetchSuccess, ErrorMessage = pcall(function()
		
		PlayerData = CurrencyData:GetAsync(tostring(Player.UserId))
		
	end)
	
if DataFetchSuccess then
		print("data fetch success")
		if PlayerData ~= nil then
		Player.CurrencyDataFolder.Coins.Value = PlayerData[1]
		Player.CurrencyDataFolder.Gems.Value = PlayerData[2]
		else
			print("not joined before")
		Player.CurrencyDataFolder.Coins.Value = 2000
		Player.CurrencyDataFolder.Gems.Value = 10
		end
		
		else
		
		Player:Kick("Data failed to load, pleaser rejoin.") --teleporter dem til et annet spill i 2 sec også tilbake uten at de merker det. (Men programer senere)
		
end


end)



--player leaving

game.Players.PlayerRemoving:Connect(function(Player)
	print("player left " .. Player.Name)
	local PlayerData = {Player.CurrencyDataFolder.Coins.Value, Player.CurrencyDataFolder.Gems.Value}
	print(Player.CurrencyDataFolder.Coins.Value)
	
	local DataSaveSuccess, DataSaveError = pcall(function()
		CurrencyData:SetAsync(tostring(Player.UserId, PlayerData))
		print("saved the data")
	end)
	
	if not DataSaveSuccess then
		print("no data save success") --always prints this
		local DataSaveAttempts = 0
		
		while DataSaveAttempts < 6 do
		wait(60)
		
		local RetrySaveSuccess, RetrySaveError = pcall(function()
		CurrencyData:SetAsync(tostring(Player.UserId), PlayerData)
		end)
	
		if RetrySaveSuccess then
		break	
		end
			
		end
		
		DataSaveAttempts = DataSaveAttempts + 1
		
	end
	print("done with the saving data")
end)
4 Likes

Caught your error.

CurrencyData:SetAsync(tostring(Player.UserId, PlayerData))

  1. You’re not saving data because you accidentally wrapped your key and data together with tostring.
  2. When you save data as a table, you don’t have to encode it in any other way.

Replace the old code with this.

local Key = Player.UserId
CurrencyData:SetAsync(Key, PlayerData)

If you’d rather keep the style you previously had, here’s what it would look like. This would also work.

CurrencyData:SetAsync(tostring(Player.UserId), PlayerData)

See if any of these work and let me know what happens.

3 Likes

I would highly recommend moving over to using Datastore 2, it is an amazing module - not too hard to learn and prevents all data-loss. For example, in scenarios .PlayerRemoving may not be fired.

1 Like

Worked great, I got to become better spotting these errors!

1 Like

Yes! As @cxuu mentioned, it is recommended that you use DataStore2 for saving data. Even though you have implemented your own backup for data save fails, data loss is always a concern. The module provides an efficient and reliable method of saving data. You can take a look at some of the examples in the post for help on updating your code.

1 Like

Do you have any examples of games that use this module?

It seems like a lot of work for me…

1 Like

Games due tend to be a lot of work! There isn’t really any quick and easy way. Just look at the examples provided on the thread and toy around with it.

I was hesitant at first to move my player data model over to Datastore 2 but it was worth it! It offers much a much better solution than most people end up with if they do it themselves.

1 Like