Trail optimization

So I basically want to create a ghost trail behind the player (Kinda like ZO but with color) and I wrote this:

local OGAgent = game.Workspace.Agent -- Character Model
local FADE_TIME = 1.5
local ONION_LAYERS_FOLDER = game.Workspace.OnionLayers

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local tweenInfo =, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local tweenGoal = {}
tweenGoal.Transparency = 1

local connection = RunService.Heartbeat:Connect(function(deltaTime)
	local newLayer = OGAgent:Clone()
	newLayer.Parent = ONION_LAYERS_FOLDER
	for i, child in pairs(newLayer:GetDescendants()) do
		if child:IsA("BasePart") or child:IsA("Decal") then
			TweenService:Create(child, tweenInfo, tweenGoal):Play()
	Debris:addItem(newLayer, FADE_TIME)

but it’s creating a lot of lag, how could I optimize this (and is cloning models really expensive)?

The reason it’s creating a lot of lag is because you’re creating a new layer once every 1/60th of a second and only destroying them 1.5 seconds later. That means in that 1.5 seconds, you have at least 90-100 objects loaded at the same time. You’re also tweening an unknown number of objects every 1/60th of a second, which is also causing its share of lag.

You can optimize this by reducing the amount of times those two events happen in a second and perhaps introduce a cap on the amount of layers.


I decided to just create all the layers beforehand then just loop through them, resulting in twice the FPS (I also added said cap on number of layer count)