Any idea why this code causes memory leaks? How can I fix it?

When checking the memory section of Lua heap this grows by 60k in like 10 minutes of playing so I guess there is a huge memory leak there. I’m not sure though I know close to nothing about scripting.

By the way. The game this is from is inferno Rush (just in case you want to check for memory leak yourself.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Client = ReplicatedStorage:WaitForChild("Client")
local Round = ReplicatedStorage:WaitForChild("Round")
local FullRoundEnd = Round:WaitForChild("FullRoundEnd")
local ReplicateMovingPart = Client:WaitForChild("ReplicateMovingPart")
local ReplicateRotatingPart = Client:WaitForChild("ReplicateRotatingPart")
local ReplicateConveyor = Client:WaitForChild("ReplicateConveyor")
local ConnectDeathPart = Client:WaitForChild("ConnectDeathPart")

local ReplicateMovingParts = {}
local Debris = {}

local function cleanUpConnections()
	for _, connection in pairs(Debris) do
		connection:Disconnect()
	end
	Debris = {}
end

ReplicateMovingPart.OnClientEvent:Connect(function(Object, Info, Props, MovesPlayer)
	local tweeninfo = TweenInfo.new(table.unpack(Info))
	local Tween = TweenService:Create(Object, tweeninfo, Props)
	Tween:Play()

	if MovesPlayer then
		local LastPosition = Object.Position
		local connection
		connection = RunService.RenderStepped:Connect(function(deltaTime)
			if Object and Object.Parent then
				local CurrentPosition = Object.Position
				local deltaPosition = CurrentPosition - LastPosition
				local Velocity = deltaPosition / deltaTime
				Object.AssemblyLinearVelocity = Velocity
				LastPosition = CurrentPosition
			else
				connection:Disconnect()
			end
		end)
		table.insert(Debris, connection)
	end
end)

ReplicateRotatingPart.OnClientEvent:Connect(function(Object, Info, Props, MovesPlayer)
	local tweeninfo = TweenInfo.new(table.unpack(Info))
	local Tween = TweenService:Create(Object, tweeninfo, Props)
	Tween:Play()

	if MovesPlayer then
		local LastOrientation = Object.Orientation
		local connection
		connection = RunService.RenderStepped:Connect(function(deltaTime)
			if Object and Object.Parent then
				local CurrentOrientation = Object.Orientation
				local deltaOrientation = CurrentOrientation - LastOrientation
				local Velocity = deltaOrientation / deltaTime
				Object.AssemblyLinearVelocity = Velocity
				LastOrientation = CurrentOrientation
			else
				connection:Disconnect()
			end
		end)
		table.insert(Debris, connection)
	end
end)

ReplicateConveyor.OnClientEvent:Connect(function(inst, Speed)
	local texture = inst:FindFirstChild("Texture")
	if texture then
		local Tween = TweenService:Create(texture, TweenInfo.new(math.abs(Speed), Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, false, 0), {
			OffsetStudsV = texture.OffsetStudsV - math.abs(Speed * 2.5)
		}):Play()

		local connection
		connection = RunService.RenderStepped:Connect(function()
			if inst and inst.Parent then
				inst.AssemblyLinearVelocity = inst.CFrame.LookVector * Speed
			else
				connection:Disconnect()
			end
		end)
		table.insert(Debris, connection)
	end
end)

ConnectDeathPart.OnClientEvent:Connect(function(Part: Part)
	local touchedConnection
	touchedConnection = Part.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Player = Players:GetPlayerFromCharacter(Hit.Parent)
			if Player and Player == LocalPlayer then
				ConnectDeathPart:FireServer()
			end
		end
	end)

	Part.AncestryChanged:Connect(function(_, parent)
		if not parent then
			touchedConnection:Disconnect()
		end
	end)
end)

FullRoundEnd.OnClientEvent:Connect(function()
	cleanUpConnections()
end)

return ReplicateMovingParts```
1 Like