I have a problem in my game where if you constantly keep adding and removing accessories, it slowly starts to lag out your client.
I have no clue what could be causing this, as I have never had to face memory leaks before, but this is just what someone told me
On the client simply clicking a button will fire a RemoteEvent
ItemFrame.Button.Activated:Connect(function()
UpdateCharacter:FireServer(page.Name)
end)
Then on the server:
local function Update(player, category, item)
local Character = player.Character
if not Character then return end
local HumanoidDescription = Character.Humanoid:GetAppliedDescription()
UpdateHumanoidDescription(player, HumanoidDescription, category, item)
Character.Humanoid:ApplyDescription(HumanoidDescription)
end
UpdateCharacter.OnServerEvent:Connect(Update)
Now I believe the problem lies in the UpdateHumanoidDescription function
local function UpdateHumanoidDescription(player, humanoidDescription, category, item)
local PlayerData = player:FindFirstChild('PlayerData')
if not PlayerData then return end
local function AccessoryToPlayerData(id, accessoryName)
local Item = id
local NewValue = Converter.Convertr(Item)
NewValue.Name = id
NewValue.Parent = PlayerData.Character[accessoryName]
end
-- Set the PlayerData values
if category then
-- Updating character
local Data = PlayerData.Character:FindFirstChild(category)
if not Data then return end
if Data:IsA('Folder') then
-- Accessory
if Data:FindFirstChild(item) then return end
if #Data:GetChildren() >= 5 then return end
-- EDIT Need to check for VIP, and see if item requires VIP
local NewItem = item
local NewValue = Converter.Convertr(NewItem)
NewValue.Name = item
NewValue.Parent = Data
else
-- Value
for i, v in pairs(CustomiseData[category]) do
-- EDIT Need to check for VIP, and see if item requires VIP
local ID -- Whether its the ID (for Shirts, Pants, Faces, etc. or Name for Body Colors
if category == 'Body Color' then
ID = v.Name
else
ID = i
end
if item == ID then
Data.Value = ID
if category == 'Bundle' then
local Details = AssetService:GetBundleDetailsAsync(ID)
end
end
end
end
else
-- Loading character
if PlayerData.Character['Roleplay Name'].Value == '' then
-- No previous data
PlayerData.Character['Roleplay Name'].Value = player.Name
PlayerData.Character.Face.Value = humanoidDescription.Face
PlayerData.Character.Shirt.Value = humanoidDescription.Shirt
PlayerData.Character.Pants.Value = humanoidDescription.Pants
-- Set default accessories
for _, id in ipairs(humanoidDescription.HatAccessory:split(',')) do
if id ~= '' then
AccessoryToPlayerData(id, 'Hats')
end
end
for _, id in ipairs(humanoidDescription.HairAccessory:split(',')) do
if id ~= '' then
AccessoryToPlayerData(id, 'Hairs')
end
end
for _, id in ipairs(humanoidDescription.FaceAccessory:split(',')) do
if id ~= '' then
AccessoryToPlayerData(id, 'Face Accessories')
end
end
for _, id in ipairs(humanoidDescription.NeckAccessory:split(',')) do
if id ~= '' then
AccessoryToPlayerData(id, 'Neck Accessories')
end
end
for _, id in ipairs(humanoidDescription.ShouldersAccessory:split(',')) do
if id ~= '' then
AccessoryToPlayerData(id, 'Shoulders Accessories')
end
end
for _, id in ipairs(humanoidDescription.FrontAccessory:split(',')) do
if id ~= '' then
AccessoryToPlayerData(id, 'Front Accessories')
end
end
for _, id in ipairs(humanoidDescription.BackAccessory:split(',')) do
if id ~= '' then
AccessoryToPlayerData(id, 'Back Accessories')
end
end
for _, id in ipairs(humanoidDescription.WaistAccessory:split(',')) do
if id ~= '' then
AccessoryToPlayerData(id, 'Waist Accessories')
end
end
end
end
-- Set HumanoidDescriptions for accessories
local HatAccessories = {}
for _, v in pairs(PlayerData.Character.Hats:GetChildren()) do
table.insert(HatAccessories, v.Name)
end
humanoidDescription.HatAccessory = table.concat(HatAccessories, ', ')
local HairAccessories = {}
for _, v in pairs(PlayerData.Character.Hairs:GetChildren()) do
table.insert(HairAccessories, v.Name)
end
humanoidDescription.HairAccessory = table.concat(HairAccessories, ', ')
local FaceAccessories = {}
for _, v in pairs(PlayerData.Character['Face Accessories']:GetChildren()) do
table.insert(FaceAccessories, v.Name)
end
humanoidDescription.FaceAccessory = table.concat(FaceAccessories, ', ')
local NeckAccessories = {}
for _, v in pairs(PlayerData.Character['Neck Accessories']:GetChildren()) do
table.insert(NeckAccessories, v.Name)
end
humanoidDescription.NeckAccessory = table.concat(NeckAccessories, ', ')
local ShouldersAccessories = {}
for _, v in pairs(PlayerData.Character['Shoulders Accessories']:GetChildren()) do
table.insert(ShouldersAccessories, v.Name)
end
humanoidDescription.ShouldersAccessory = table.concat(ShouldersAccessories, ', ')
local FrontAccessories = {}
for _, v in pairs(PlayerData.Character['Front Accessories']:GetChildren()) do
table.insert(FrontAccessories, v.Name)
end
humanoidDescription.FrontAccessory = table.concat(FrontAccessories, ', ')
local BackAccessories = {}
for _, v in pairs(PlayerData.Character['Back Accessories']:GetChildren()) do
table.insert(BackAccessories, v.Name)
end
humanoidDescription.BackAccessory = table.concat(BackAccessories, ', ')
local WaistAccessories = {}
for _, v in pairs(PlayerData.Character['Waist Accessories']:GetChildren()) do
table.insert(WaistAccessories, v.Name)
end
humanoidDescription.WaistAccessory = table.concat(WaistAccessories, ', ')
-- Body sizes (need to base sizes on age)
local Size = AgeSizes[PlayerData.Character.Age.Value]
humanoidDescription.DepthScale = Size
humanoidDescription.HeadScale = Size
humanoidDescription.HeightScale = Size
humanoidDescription.WidthScale = Size
humanoidDescription.BodyTypeScale = 0
humanoidDescription.ProportionScale = 0
-- Face/Shirt/Pants
humanoidDescription.Face = PlayerData.Character.Face.Value
humanoidDescription.Shirt = PlayerData.Character.Shirt.Value
humanoidDescription.Pants = PlayerData.Character.Pants.Value
-- BodyColor
humanoidDescription.HeadColor = BrickColor.new(PlayerData.Character['Body Color'].Value).Color
humanoidDescription.LeftArmColor = BrickColor.new(PlayerData.Character['Body Color'].Value).Color
humanoidDescription.LeftLegColor = BrickColor.new(PlayerData.Character['Body Color'].Value).Color
humanoidDescription.RightArmColor = BrickColor.new(PlayerData.Character['Body Color'].Value).Color
humanoidDescription.RightLegColor = BrickColor.new(PlayerData.Character['Body Color'].Value).Color
humanoidDescription.TorsoColor = BrickColor.new(PlayerData.Character['Body Color'].Value).Color
return humanoidDescription
end
It’s a lotta code because I have to convert values into strings to put in the HumanoidDescription for each individual accessory type. But if anyone knows where a certain section could be causing this