Datastore2 "Value not changed as it wasn't updated"

I’m trying to make a script which saves player data whenever they leave/join via datastore (cause apparently it’s more reliable) (with the help of this tutorial by okeansky), however whenever I attempt to save the changed data, I get a warning reading: “Data store Gold was not saved as it was not updated.” despite the fact that the value has been changed

here’s my code:

local DataStore2 = require(1936396537)
DataStore2.Combine("MasterKey", "Level", "XP")

local DefaultRank = 0
local DefaultXP = 0
local DefaultGold = 0

local xpToLevelUp = function(Rank)
	return (((Rank*(Rank - 1))/15) * 100) + 100
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local GoldValue = Instance.new("IntValue", leaderstats)
	GoldValue.Name = "Gold"
	
	local UnbankedValue = Instance.new("IntValue", leaderstats)
	UnbankedValue.Name = "UnbankedGold"
	
	local RankValue = Instance.new("IntValue", leaderstats)
	RankValue.Name = "Rank"
	
	local XPValue = Instance.new("IntValue", leaderstats)
	XPValue.Name = "XP"
	
	local XPMaxValue = Instance.new("IntValue", leaderstats)
	XPMaxValue.Name = "XPMax"
	
	-- Get datastores
	
	local GoldStore = DataStore2("Gold", player)
	local RankStore = DataStore2("Rank", player)
	local XPStore = DataStore2("XP", player)
	
	-- Functions when Rank + XP + Gold Change
	
	local function UpdateGold(Gold)
		player.leaderstats.Gold.Value = Gold
	end
	
	local function UpdateRank(Rank)
		player.leaderstats.Rank.Value = Rank
	end
	
	local function UpdateXP(XP)
		if XP >= xpToLevelUp(XPStore:Get(DefaultRank)) then
			XPStore:Increment(xpToLevelUp(RankStore:Get(DefaultRank)) * -1)
			XPMaxValue.Value = xpToLevelUp
			RankStore:Increment(1)
		else
			player.leaderstats.XP.Value = XP
		end
	end
	
	
	-- Call functions at once
	UpdateRank(RankStore:Get(DefaultRank))
	UpdateXP(XPStore:Get(DefaultXP))
	UpdateGold(GoldStore:Get(DefaultGold))
	
	-- Call functions again on updates
	
	RankStore:OnUpdate(UpdateRank)
	XPStore:OnUpdate(UpdateXP)
	GoldStore:OnUpdate(UpdateGold)
end)
1 Like