Can't save Data with Profile Service

Here’s a section of my script, please don’t stop reading because of the first script’s length, it’s just there for context of what i’m doing. I’m creating a Folder inside the Player with Values representing the player’s data, and when a value changes, i want to update the data. Everything works, except for the updating the data part

local PlayerAdded = function(Player)
	-- Get's player's profile.Data
	local PlayerData = (DataManager.GetData(Player)).Data
	-- Create folder that represent's PlayerData.Data
	local DataFolder = Instance.new("Folder")
	DataFolder.Name = "DataFolder"
	DataFolder.Parent = Player
	
	-- no need to read this except for the last function, it works as intended
	for Name, BigBoy in pairs(PlayerData) do
		
		local NewValue, ParentFolder
		-- If Player.Data has a table inside it, creates another SubFolder representing it
		if typeof(BigBoy) == "table" then
			
			local NewFolder = Instance.new("Folder")
			NewFolder.Name = Name
			NewFolder.Parent = DataFolder
			-- Creates Value instance inside SubFolder
			for Key, Value in pairs(BigBoy) do
				
				NewValue = CreateNewValueInstance(NewFolder, Key, Value)
				ParentFolder = NewFolder.Name
				
			end
		end
		
		NewValue.Changed:Connect(function()
			-- This is the part that doesn't work
			PlayerStatChange(NewValue, PlayerData, ParentFolder)

		end)
	end
end

Result:
imagem_2023-10-01_190902647
This is all that i have put into the player Data so far, so it’s working perfectly

Now for the .Changed event

-- Let's say i change the "Strength" value from 1 to 2, this script would run
local PlayerStatChange = function(NewValue, PlayerData, ParentFolder)
	-- The variables would have the following values:
	-- Strength(Instance), My Profile.Data, "Statistics"

	local ChangedData =  PlayerData[ParentFolder][NewValue.Name] -- This is the same as = Profile.Data.Statistics.Strength
	
	-- This print's out the Players Strength as it should
	print(ChangedData) -- 1

	-- Now this line should change it to 2 (i think), but it doesn't
	ChangedData = NewValue.Value
	
end

Tf do i do?

Also this is the DefaultTemplate

local DefaultTemplate = {Statistics = {Strength = 1, Stamina = 1},}

Turns out the problem is entirely inside this function

NewValue.Changed:Connect(function()
	-- Can't make changes to the ProfileService profiles inside this function for some reason
	PlayerStatChange(NewValue, PlayerData, ParentFolder)

end)
local PlayerStatChange = function(NewValue, PlayerData, ParentFolder)
	local ChangedData =  PlayerData[ParentFolder][NewValue.Name] -- This is the same as = Profile.Data.Statistics.Strength
	ChangedData = NewValue.Value -- Which means this line doesn't do anything
end

Anyone know the reason?

I moved the .Changed function to another script and now it works. No idea what happened, didn’t learn anything from this experience, god bless

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