Datastore not saving

hello, i made a datastore script to save some boolvalues. but the issue is that some of them save and some dont. here’s the script

local datastore = game:GetService("DataStoreService")
local ds = datastore:GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local id = "id_"..plr.userId
	local TokenSave = plr.leaderstats.FazTokens
	local GemSave = plr.leaderstats.Gems
	local FreddySave = plr.Values.FreddyPurchased
	local BonnieSave = plr.Values.BonniePurchased
	local ChicaSave = plr.Values.ChicaPurchased
	local FoxySave = plr.Values.FoxyPurchased
	local GFSave = plr.Values.GFPurchased
	local PFSave = plr.Values.PFPurchased
	local MutantFreddySave = plr.Values.MutantFreddyPurchased
	local MutantBonnieSave = plr.Values.MutantBonniePurchased
	local MutantChicaSave = plr.Values.MutantChicaPurchased
	local MutantFoxySave = plr.Values.MutantFoxyPurchased
	local FBBSave = plr.Values.FBBPurchased
	local SantaFreddySave = plr.Values.SantaFreddyPurchased
	local FrostBonnieSave = plr.Values.FrostBonniePurchased
	local ElfChicaSave = plr.Values.ElfChicaPurchased
	local CandyCaneFoxySave = plr.Values.CandyCaneFoxyPurchased
	local AnimBoughtSave = plr.Values.AnimatronicsBought
	
	local data
	local success, errormessage = pcall(function()
		data = ds:GetAsync(id)
	end)
	
	if success then
		if data then
			TokenSave.Value = data.cash
			GemSave.Value = data.gem
			FreddySave.Value = data.freddyData
			BonnieSave.Value = data.bonnieData
			ChicaSave.Value = data.chicaData
			FoxySave.Value = data.foxyData
			GFSave.Value = data.gfData
			PFSave.Value = data.pfData
			MutantFreddySave.Value = data.mutantfreddyData
			MutantBonnieSave.Value = data.mutantbonnieData
			MutantChicaSave.Value = data.mutantchicaData
			MutantFoxySave.Value = data.mutantfoxyData
			FBBSave.Value = data.fbbData
			SantaFreddySave = data.santafreddyData
			FrostBonnieSave = data.frostbonnieData
			ElfChicaSave = data.elfchicaData
			CandyCaneFoxySave = data.candycanefoxyData
			AnimBoughtSave.Value = data.animsboughtData
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local id = "id_"..plr.userId
	local data = {
		cash = plr.leaderstats.FazTokens.Value;
		gem = plr.leaderstats.Gems.Value;
		freddyData = plr.Values.FreddyPurchased.Value;
		bonnieData = plr.Values.BonniePurchased.Value;
		chicaData = plr.Values.ChicaPurchased.Value;
		foxyData = plr.Values.FoxyPurchased.Value;
		gfData = plr.Values.GFPurchased.Value;
		pfData = plr.Values.PFPurchased.Value;
		mutantfreddyData = plr.Values.MutantFreddyPurchased.Value;
		mutantbonnieData = plr.Values.MutantBonniePurchased.Value;
		mutantchicaData = plr.Values.MutantChicaPurchased.Value;
		mutantfoxyData = plr.Values.MutantFoxyPurchased.Value;
		fbbData = plr.Values.FBBPurchased.Value;
		santafreddyData = plr.Values.SantaFreddyPurchased.Value;
		frostbonnieData = plr.Values.FrostBonniePurchased.Value;
		elfchicaData = plr.Values.ElfChicaPurchased.Value;
		candycanefoxyData = plr.Values.CandyCaneFoxyPurchased.Value;
		animsboughtData = plr.Values.AnimatronicsBought.Value;
	}
	
	local success, errormessage = pcall(function()
		ds:SetAsync(id, data)
	end)
	
	if success then
		print("Success")
	else
		print("Failed")
	end
end)

the ones that dont save are :
santa freddy, frost bonnie, elf chica and candy cane foxy
also, there isnt any error in the output so i dont know what the problem is.

You forgot .Value from the ones you’re not getting data from in the PlayerAdded

oh my god, theres no way… thank you!!!

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

game.Players.PlayerAdded:Connect(function(Player)
	local Id = "id_" .. Player.userId
	local Success, ErrorMessage = pcall(function()
		local Data = DataStore:GetAsync(Id)
		if Data then
			Player.leaderstats.FazTokens.Value = Data.cash
			Player.leaderstats.Gems.Value = Data.gem
			
			-- Loop through all the values in the table of purchased animatronics
			for AnimName, Purchased in pairs(Data.anims) do
				Player.Values[AnimName].Value = Purchased
			end
		end
	end)
	
	if not Success then
		warn(ErrorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Id = "id_" .. Player.userId
	local Success, ErrorMessage = pcall(function()
local animsTable ={}
for i, v in ipairs(Player.Values:GetChildren) do
animsTable[v] = v.Value
end
		local Data = {
			cash = Player.leaderstats.FazTokens.Value,
			gem = Player.leaderstats.Gems.Value,
			anims = animsTable
		}
		DataStore:SetAsync(Id, Data)
	end)
	
	if not Success then
		warn(ErrorMessage)
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.