Currency System's dataStore isn't loading / saving

Hello there! I wanted a currency system that saves with a dataStore. I followed a tutorial online by AlvinBlox to get my script. I have checked if I have toggled the API checkbox - thing. Which I have toggled it.

The issue is that the datastore isn’t loading any new values for my Coins. For example, if I pick up 5 coins in my game, close it, then re-open, it would set it to the default starter amount.

I have looked for solutions to this, but I haven’t found much. Every time I tried to fix something, it basically just got worse.

I want to know if I’m doing anything wrong with my script. Any help or feedback is greatly appreciated!

local currencyName = "Coins"

local DataStore = game:GetService("DataStoreService"):GetDataStore("CoinDataStore")

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	

	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

	local ID = currencyName.."-"..player.UserId
	local savedData = nil	
	
	pcall(function(player)
		
		savedData = DataStore:GetAsync(ID)
		
	end)

	if savedData ~= nil then
		
		currency.Value = savedData
		print("Data loaded")
		
	else
		
		currency.Value = 2500
		print("New player to the game")
		
	end


end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
	
end)

game:BindToClose(function()

	for i, player in pairs(game.Players:GetPlayers()) do
		
		if player then
			
			player:Kick("This game is shutting down! Please rejoin.")
			
		end
		
	end

	wait(5)	

end)

A couple things;

  1. You should always pcall any communications with roblox APIs (you forgot to in the player remove function)

  2. You can remove the bind to close part, delaying the game shutdown by 5 seconds does not help. There used to be a problem with datastore where you would have to implement bind to close logic for it to work on the last player. To my knowledge, this issue no longer exists.

  3. Do you have any errors?

  4. The killer of all datastore testing. I have done this countless times. When you add the currency to the intvalue, make sure you are doing it from the SERVER side, not the CLIENT side. If you add it from the client side, the server will not be able to read it, thus it will not save.

  5. Theres no need to have player as an argument in the pcall function. It will never be assigned by pcall.

Can we see your script that gives coins, also you don’t pass anything to pcall

I have no errors, that’s why this is confusing me…

This is my updated script, taking your advice. It didn’t work, so the main problem might be number 4.

I’m not exactly sure how to implement number 4 into the script, however. Any help would be greatly appreciated!

local currencyName = "Coins"

local DataStore = game:GetService("DataStoreService"):GetDataStore("CoinDataStore")

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	

	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

	local ID = currencyName.."-"..player.UserId
	local savedData = nil	
	
	pcall(function()
		
		savedData = DataStore:GetAsync(ID)
		
	end)

	if savedData ~= nil then
		
		currency.Value = savedData
		print("Data loaded")
		
	else
		
		currency.Value = 2500
		print("New player to the game")
		
	end


end)

game.Players.PlayerRemoving:Connect(function(player)
	
	pcall(function()
		
		local ID = currencyName.."-"..player.UserId
		DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
		
	end)
	
end)

When ever I wrap a function in a pcall it wont output any errors, so maybe try something like this:

local success, err =  pcall(function()
		savedData = DataStore:GetAsync(ID)
end)

if not success then
	warn(err)
end
1 Like

DOes your script print “new player to the game” each time, or is it printing data loaded?

If I change the script, then it prints “new player added to the game”. After that it will always print “data loaded”.

No errors pop up when I try that…

If it is printing data loaded, then the #4 that I listed is your problem. Follow these two steps to not make that mistake:

  1. If youre changing the currency value manually, click this “client” button on top of your screen, to view the game from the servers perspective.

  2. If youre changing the the currency value from a script, make sure it is a SERVER script, not a LOCAL script.

So I tried your script in a new game I made to see what the problem was exactly. I found nothing wrong with it

Exactly. The problem is that he is updating the currency on the client, so the server cant read it when its saving.

2 Likes

Some added info relating to this…

1: The script is in ServerScriptService, if that’s what you are talking about.

2: The script is a normal script: Not local.

Plus, I have tried manually setting the currency to a different amount in the game server (not the client) but it still did not work…

Is datastore service enabled? If so, please send the script youre using to update players currency so I can double check it.

So I had this issue too. I think it has to do with not getting enough time to save your data, for example, In game the Roblox server stays open for 30 seconds (plenty of time to save data), but in Studio the server closes almost immediately (not nearly enough time to save data). You could make a Autosave system, or in game it should work.

You can use RunService:IsStudio() to determine whether it is studio or not, and then yield.
I wrote about it here.

Hi Guys, I was having various issues with coins + data store + leader stats etc. Took a while but I found a solution that works for me and may be helpful to you as well. Its a coin system with different valued coins up to “100”. The model has $5850 worth of coins for you to spread around your map. It comes with detailed instructions and all coins collected add up properly and save for your next visit!
(9) CoinCollectingAndSavingSystem - Roblox

1 Like