Problem with datasaving(Profile service and attributes)

I am trying to update the profile data together with the change in attribute. For some reason it is not working. When I change the attribute the getAttributeSignal event is not running. Here is the code:


local PSTORE = ProfileService.GetProfileStore("Production", Template)

local function Attributes(plr, data)
	data:SetAttribute("IsTerrifier", false)
	data:SetAttribute("IsSurvivor", false)
	data:SetAttribute("KilledPlayer", 0)
	data:SetAttribute("MinigamesDone", 0)
end

local function OnPlayerAdded(plr)
	local profile = PSTORE:LoadProfileAsync("Player__" .. plr.UserId)
	if profile == nil then
		plr:Kick("Unable to fetch data")
		return
	end
	profile:AddUserId(plr.UserId)
	profile:Reconcile()
	profile:ListenToRelease(function()
		Module.Profiles[plr] = nil
		plr:Kick("Data loaded remotely. Please rejoin")
	end)

	if plr:IsDescendantOf(Players) then
		Module.Profiles[plr] = profile
			local data = plr.Character:WaitForChild("Data")
			Attributes(plr, data)
			for attribute, value in pairs(profile.Data) do
				print(attribute)
				if attribute ~= "Inv" then
					data:SetAttribute(attribute, value)
					data:GetAttributeChangedSignal(attribute):Connect(function()
						profile.Data[attribute] = data:GetAttribute(attribute)
					end)
				else
					Module.Inventories[plr] = profile.Data.Inv
				end
			end
	else
		profile:Release()
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Players.PlayerRemoving:Connect(function(plr)
	local profile = Module.Profiles[plr]
	if profile then
		profile:Release()
	end
end)

May i know the output everytime new player join ?
is this on other script?
image

image
There is no issue when the player joins. Its just that the GetAttributessignal is not running during the game

image

Wow under character yeah … if im not mistaken , everytime you respawn you have to load data again ,

Yeah it is parented under character

local function Setup(data, profile, attributeName, value)
	data:SetAttribute(attributeName, value)

	local connection = data:GetAttributeChangedSignal(attributeName):Connect(function()
		print(tostring(attributeName) .. " detected")
		local newValue = data:GetAttribute(attributeName)
		profile.Data[attributeName] = newValue
	end)

	return connection
end

local function Initialize(data)
	return {
		IsTerrifier = false,
		IsSurvivor = false,
		KilledPlayer = 0,
		MinigamesDone = 0
	}
end

local function LoadData(player, profile)
	local connections = {}

	local function handleCharacter(character)
		local data = character:WaitForChild("Data")

		for _, connection in ipairs(connections) do
			connection:Disconnect()
		end
		table.clear(connections)

		local defaultAttributes = Initialize()
		for name, value in pairs(defaultAttributes) do
			table.insert(connections, Setup(data, profile, name, value))
		end

		for attribute, value in pairs(profile.Data) do
			if attribute ~= "Inv" then
				table.insert(connections, Setup(data, profile, attribute, value))
			else
				Module.Inventories[player] = value
			end
		end
	end

	if player.Character then
		handleCharacter(player.Character)
	end

	player.CharacterAdded:Connect(handleCharacter)
end

Well what i can say it could be …

  1. no respawn handler
  2. no verification that signals were atcually connected

So i seperated the function and added connection to track attributes and respawn handling .
So everytime you respawn it will re-setattribute and clear connection and re-insert to prevent memory leak.

Problems were with the connection thank you. I can fix it on my own now

1 Like

Thank you for your time. I was having hard time solving this.

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