Datastore 2, keystore not replicating to my player data folder

I’m trying to give the player cash and the keystore is being updated (i printed keystore:Get() ). But the replicated amount in Player’s Data folder is not being update for some reason. Can anyone help?

Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local DataStore2 = require(script:WaitForChild("DataStore2"))
local LevelModule = require(script:WaitForChild("Level"))

local DataVars = {
	["Cash"] = 100,
	["Hunger"] = 100,
	["Level"] = 1,
	["XP"] = 0,
	["CO2"] = 0

}

local CarVars = {
	["Mercedes-Benz S Class"] = false,
	["Toyota Corolla"] = false,
	["Honda Vezel"] = false
}


function addPlayer(player)
	local DataFolder = Instance.new("Folder",player)
	DataFolder.Name = "Data"
	
	--Loop to make rep amounts
	for key, defaultval in pairs(DataVars) do
	local keystore = DataStore2(key, player)
    local RepAmt = Instance.new("NumberValue",DataFolder)
	RepAmt.Name = key
	
	local function UpdateRepAmt(RepAmt)
	RepAmt.Value = keystore:Get(defaultval)
	end
	
	UpdateRepAmt(RepAmt)
	keystore:OnUpdate(UpdateRepAmt(RepAmt))
	print(key.." loaded")
	end

	--Loop to make car rep amounts
	local CarFolder = Instance.new("Folder",DataFolder)
	CarFolder.Name = "Cars"
	
	for ckey, cdefaultval in pairs(CarVars) do
	local ckeystore = DataStore2(ckey, player)
    local cRepAmt = Instance.new("BoolValue",CarFolder)
	cRepAmt.Name = ckey
	
	local function UpdateRepAmt(cRepAmt)
	cRepAmt.Value = ckeystore:Get(cdefaultval)
	end
	
	UpdateRepAmt(cRepAmt)
	ckeystore:OnUpdate(UpdateRepAmt(cRepAmt))
	print(ckey.." loaded")
	end
	
	
end



for _,player in pairs(Players:GetPlayers())do
    addPlayer(player)
end

Players.PlayerAdded:connect(addPlayer)


1 Like

loops alot, dont rely on it

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

game.Players.PlayerAdded:Connect(function(player)
	local key = "player-"..player.UserId
	
	local savedValues = DataStore:GetAsync(key)
		
	local leaderstatsFolder = Instance.new("Folder", player)
	leaderstatsFolder.Name = "leaderstats"
	
	if(savedValues) then		
		for i,v in pairs(script.leaderstats:GetChildren()) do
			local stat = Instance.new(v.ClassName, player.leaderstats)
			stat.Name = v.Name
		end
		
		for i,v in pairs(player.leaderstats:GetChildren()) do
			v.Value = savedValues[i]
		end
	else	 
		local save = {} 
		for i,v in pairs(script.leaderstats:GetChildren()) do
			local stat = Instance.new(v.ClassName, player.leaderstats)
			stat.Name = v.Name
			table.insert(save, stat.Value)
		end
		DataStore:SetAsync(key, save)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local key = "player-"..player.UserId
	
	local save = {}
	
	for i,v in pairs(player.leaderstats:GetChildren()) do
		table.insert(save, v.Value)
	end
	DataStore:SetAsync(key, save)
	
end)

and also, it does have some…issues, but can be fixed, also there should be values parented to . that you want to save. though your method is a bit better than this one,

go there, and look at the solution , it could help you out

1 Like

What do you mean values parented to

Also, doesn’t datastore2 auto save when player leaves?

Fixed it lol

	local function UpdateRepAmt(updatedValue)
		RepAmt.Value = keystore:Get(updatedValue)
	end
	
	UpdateRepAmt(defaultval)
	keystore:OnUpdate(UpdateRepAmt)

problem was this update function