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)
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 …
no respawn handler
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.