Datastore failing to save value's

The Problem:

I had a group of developers commissioned for developing a game I’m working on releasing shortly. I was running through making sure everything is functionable for release. After conducting some tests, I noticed that the coins value is not saving. I am not sure about EXP, as EXP has not been set up yet. But I would assume that if the coins value is not saving, neither would the EXP value.

I personally can’t find anything wrong in the script that would be preventing the coins value from being saved, and no errors occurred in the dev console. Any help towards the right direction here would be greatly appreciated.

--// Services
local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")

--//Vars
local CoinsDatastore = DatastoreService:GetDataStore("Coins")
local ExpDatastore = DatastoreService:GetDataStore("Exp")

--//Datastore Function
local function RetrievePlayerData(Player : Player)
	local UserId = Player.UserId
	return CoinsDatastore:GetAsync(UserId), ExpDatastore:GetAsync(UserId)
end

--//Startup
local function PlayerSetup(Player : Player)
	local HiddenFolder = Instance.new("Folder")
	local CoinsValue = Instance.new("IntValue")
	local EXPValue = Instance.new("IntValue")
	
	
	HiddenFolder.Name = "Stats"
	CoinsValue.Name = "Coins"
	EXPValue.Name = "Experience"
	
	local succ, Coins, Exp = pcall(RetrievePlayerData(Player))
	if Coins and Exp then
		CoinsValue.Value = Coins
		EXPValue.Value = Exp
	end
	
	CoinsValue.Parent = HiddenFolder
	EXPValue.Parent = HiddenFolder
	HiddenFolder.Parent = Player
	
	CoinsValue.Changed:Connect(function()
		local succ, err = pcall(function()
			CoinsDatastore:SetAsync(Player.UserId, CoinsValue.Value)
		end)
	end)
	EXPValue.Changed:Connect(function()
		local succ, err = pcall(function()
			ExpDatastore:SetAsync(Player.UserId, EXPValue.Value)
		end)
	end)
end

Players.PlayerAdded:Connect(PlayerSetup)

It doesnt seem that you have :GetAsync(), maybe add that? or it isnt used properly

CoinsValue.Value = CoinsDatastore:GetAsync(UserId), CoinsValue.Value) 
1 Like

That’s not how pcall works. Rewrite it like so:
local succ, Coins, Exp = pcall(RetrievePlayerData, Player)

1 Like

Still doesn’t appear to be saving the data.

Are you changing the data in a Script, or a LocalScript?

All script’s that are affiliated with changing the data are server sided.

I advise you stay away from setting data upon change of values, as rapid changes or a high amount of player volume in a single server can overload the DataStore and cause data issues. I suggest doing this upon PlayerRemoving. Also, you should just get rid of the pcall functions altogether. Some scripters like to use them, but I think in this case it should only error if something is very wrong.

2 Likes

Yeah, I think I’ll just end up rewriting the entire script tonight or tomorrow. I wanted to avoid doing that, but at this point it’s just where it’s at.

Appreciate the help everybody!